Section 7.1. Regular Expressions as a Language Component


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 overflowing 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 7-1. Overview of Perl's Regex-Related Items

Regex-Related Operators

m/ regex/mods (˜306)

s/ regex/replacement/mods (˜318)

qr/ regex/mods (˜303)

split (‹) (˜321)

Related Pragmas

use charnames ':full'; (˜290)

use overload; (˜341)

use re 'eval'; (˜337)

use re 'debug'; (˜361)

Related Functions

lc lcfirst uc ucfirst (˜290)

pos (˜313) quotemeta (˜290)

reset (˜308) study (˜359)

Modifiers

Modify How ...

/x /o

regex is interpreted (˜292, 348)

/s /m /i

engine considers target text (˜292)

/g /c /e

other (˜311, 315, 319)

After-Match Variables (˜299)

$1, $2 , etc.

captured text

$^N $+

latest/highest filled $1 , $2 , ...

@- @+

arrays of indices into target

$' $& $'

text before, of, and after match

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

(best to avoidsee "Perl Efficiency Issues" ˜356)

Related Variables

$_

default search target (˜308)

$^R

embedded-code result (˜302)


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 codeyou've just hit another special case you weren't aware of. [ ] 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 .

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



Mastering Regular Expressions
Mastering Regular Expressions
ISBN: 0596528124
EAN: 2147483647
Year: 2004
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net