Previous Section  < Free Open Study >  Next Section

7.1 Regular Expressions as a Language Component

An attractive feature of Perl is that regex support is so deftly built in as part of the language. Rather than providing stand-alone functions for applying regular expressions, Perl provides regular-expression operators that are meshed well with the rich set of other operators and constructs that make up the Perl language.

With as much regex-wielding power as Perl has, one might think that it's over- flowing with different operators and such, but actually, Perl provides only four regex-related operators, and a small handful of related items, shown in Table 7-1.

Table 1. Overview of Perl's Regex-Related Items

Regex-Related Operators

m/ regex / mods (see Section 7.4.3)

s/ regex / replacement / mods (see Section 7.6)

qr/ regex / mods (see Section 7.4)

split(···) (see Section 7.7)

ModifiersModify How . . .

/x /oregex is interpreted(see Section 7.2.3, Section 7.9.1)

/s /m /iengine considers target text (see Section 7.2.3)

/g /c /eother (see Section 7.5.3.3, Section 7.5.4.4, Section 7.6.2)

Related Pragmas

use charnames 'full';(see Section 7.2.1.1)

use overload;(see Section 7.8.6)

use re 'eval';(see Section 7.8.3)

use re 'debug';(see Section 7.9.6)

After-Match Variables (see Section 7.3.3)

$1, $2, etc.captured text

$^N $+latest/highest filled $1, $2, . . .

@- @+arrays of indices into target

----------------------------------------------------------

$' $& $'text before, of, and after match

(best to avoid—see "Perl Efficiency Issues" Section 7.9.3.3)

Related Functions

lc lcfirst uc ucfirst (see Section 7.2.1.1)

pos (see Section 7.5.4.1) quotemeta (see Section 7.2.1.1)

reset (see Section 7.5.1.4) study (see Section 7.9.4)

Related Variables

$_default search target (see Section 7.5.2.1)

$^Rembedded-code result (see Section 7.3.3)

Perl is extremely powerful, but all that power in such a small set of operators can be a dual-edged sword.

7.1.1 Perl's Greatest Strength

The richness of variety and options among Perl's operators and functions is perhaps its greatest feature. They can change their behavior depending on the context in which they're used, often doing just what the author naturally intends in each differing situation. In fact, O'Reilly's Programming Perl goes so far as to boldly state "In general, Perl operators do exactly what you want...." The regex match operator m/ regex /, for example, offers an amazing variety of different functionality depending upon where, how, and with which modifiers it is used.

7.1.2 Perl's Greatest Weakness

This concentrated richness in expressive power is also one of Perl's least-attractive features. There are innumerable special cases, conditions, and contexts that seem to change out from under you without warning when you make a subtle change in your code—you've just hit another special case you weren't aware of.[2] The Programming Perl quote in the previous paragraph continues "...unless you want consistency." Certainly, when it comes to computer science, there is a certain appreciation to boring, consistent, dependable interfaces. Perl's power can be a devastating weapon in the hands of a skilled user, but it sometimes seems with Perl, you become skilled by repeatedly shooting yourself in the foot.

[2] That they're innumerable doesn't stop this chapter from trying to cover them all!

    Previous Section  < Free Open Study >  Next Section