Home arrow Perl Programming arrow Page 3 - Completing Regular Expression Basics

Subroutine-Based Usage - Perl

In this conclusion to a four-part series on parsing and regular expression basics in Perl, we finish our study of regular expressions; you'll even learn how to create your own. This article is excerpted from chapter one of the book Pro Perl Parsing, written by Christopher M. Frenz (Apress; ISBN: 1590595041).

  1. Completing Regular Expression Basics
  2. Universal Flags
  3. Subroutine-Based Usage
  4. Creating Your Own Expressions
By: Apress Publishing
Rating: starstarstarstarstar / 4
June 10, 2010

print this article



In addition to the standard usage, you can also access the functionality of this module through a subroutine-based interface, which allows you to perform a matching operation with a syntax similar to a procedural call. If you were to recode the previous example using this alternative syntax, it would look like Listing 1-9.

Listing 1-9. Pulling Quotes Out via a Subroutine

#!/usr/bin/perl -w
use Regexp::Common 'RE_ALL';

    $_ =~ RE_delimited(-delim=>'"',-keep)
    and print "$1\n";

You should note several important things here if you choose to use this syntax instead. The first is that when you call theRegexp::Commonmodule, you must appendRE_ALLto the end of the line so Perl is able to recognize the alternative syntax. Without this, you will receive a compilation error that says the subroutines are undefined. The second noteworthy thing is that you must explicitly write$_=~in order to perform the required matching operation. Lastly, you should also note that the flags are read in as arguments separated by commas. Accessing the regular expressions this way can lead to faster execution times since this method does not return objects to be interpolated but, rather, actual regular expressions.

In-Line Matching and Substitution

I will cover these two methods together since they have similar syntax and use an object-oriented interface. In terms of basic pattern matching, they offer no real advantage other than allowing you to create code that may be somewhat more user-friendly to read; their syntax is as follows:

    print "$SomeNumber is an Integer";

This interface allows you to easily perform substitutions on a string without changing the original string. For example:


In this case,$SubstitutedStringis a new string that is going to be assigned the value of the$Originalstring with all substitutions already made, and the$Substitutionstring specifies the string that is going to be put in place of the characters that were able to match the pattern.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: