Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: The value of pattern match variable $&

by McDarren (Abbot)
on May 15, 2006 at 10:17 UTC ( #549425=note: print w/replies, xml ) Need Help??

in reply to The value of pattern match variable $&

I don't have my copy handy, but I'm almost certain that PBP recommends against using $`, $' and $&. Mainly because of the fact that they are global, and if you have several pattern matches in your code you can never be sure which variable was set by which match.

Much better instead to use explicit locally-scoped variables, and capturing parentheses, eg:

my ($prematch, $match, $postmatch) =~ /^(foo)(bar)(baz)$/;

Darren :)

Replies are listed 'Best First'.
Re^2: The value of pattern match variable $&
by spiritway (Vicar) on May 15, 2006 at 14:34 UTC

    IIRC, there is also a big performance hit, if any of these appear anywhere in your program.

      Regexps without captures are faster than regexps with captures. Using any of those three variables causes all regexps in that interpreter instance (similiar to "in that process") to behave as if they had captures. That slows down all regexps without captures.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://549425]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-05-17 15:05 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (66 votes). Check out past polls.