in reply to RegEx Help - Look Behind

The gem from the perlre page (which prasadbabu pointed you to) is to stay away from $& - it reads this:
Once perl sees that you need one of $&, $` or $' anywhere in the program, it has to provide them on each and every pattern match. This can slow your program down. The same mechanism that handles these provides for the use of $1, $2, etc., so you pay the same price for each pattern that contains capturing parentheses. But if you never use $&, etc., in your script, then patterns without capturing parentheses won't be penalized. So avoid $&, $', and $` if you can, but if you can't (and some algorithms really appreciate them), once you've used them once, use them at will, because you've already paid the price.

As I understand it, this means that from the point you invoke one of those evil 3, your program will cache (in your precious RAM) every text match it makes while the program is running - if you're parsing a large amount of data that's *nasty*!

There is no need in this case (and I don't recall ever finding a case that did need) to use $& - what you want is a specific part of the match (for that you could use round brackets) like this:

#!/usr/bin/perl -w use strict; my $string = qq{oCMenu.makeMenu('m40','','Files','','',100,20) oCMenu. +makeMenu('m41','m40','Periodic Reports','index.cfm?openaction=file_ar +chive.view&CONTENT_ID=BF764E84-ED11-EC57-40672E520082E823','',125,20) + oCMenu.makeMenu('m53','m40','Reference Documents','index.cfm?openact +ion=file_archive.view&CONTENT_ID=BF76E870-E7CC-515F-D4D5C3D4A210BB9A' +,'',125,20)}; $string =~ /Periodic Reports','([^']+)'/; print "Want to see:\n", "index.cfm?openaction=file_archive.view&CONTENT_ID=BF764E84-ED11-EC57- +40672E520082E823", "\n$1\n";
Here the $1 returns the portion between the first () in the match ($2 will return the second and so on...)

Replies are listed 'Best First'.
Re^2: RegEx Help - Look Behind
by dcd (Scribe) on Dec 03, 2005 at 16:39 UTC
    I've had a different interpretation than serf on this. I believe that there is little of the "precious RAM" that is used, (unless that matches were large), since there are only three $&, $' and $` variables. I've always thought that it was more the slightly extra cpu time (hence the "can slow your program down"). I've actually thought that the authors were just trying to be truthful, and tell you what was going on, and rather to pay attention to the sentence that was cut off So avoid $&, $’, and $‘ if you can, but if you can’t (and some algorithms really appreciate them), once you’ve used them once, use them at will, because you’ve already paid the price.  As of 5.005, $& is not so costly as the other two.