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).
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';
while(<>){ $_ =~ 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:
if($RE{num}{int}->matches($SomeNumber)){ 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.