On Mon, 2 Aug 2004, Bob Showalter wrote:
sudhindra k s wrote:Hi
Can someone please tell me what the following code is trying to do
if ( /CR List/ .. /\(2\)/ ) { if ( /CR List/ ) { print " $'\n"; } elsif ( /\(2\)/ ) {print " $` \n" ;} else { print " $_\n";} }
I know that it is trying to print whatever is there between CR List and (2). But i am not able guess more than that.
The $' and $` are builtin Perl variables. They are documented in
perldoc perlvar
$' is the text following the last successful match (i.e. the stuff on the line to the right of "CR List"). $` is the text preceeding the last successful match (the stuff on the line to the left of "(2)")
And according to several reliable sources of documentation, notably _Mastering Regular Expressions_, they should *never be used*.
The presence of these variables anywhere in your code changes the pattern matching strategy that Perl uses in such a way that performance is much, much worse than normal in most cases.
The whys and hows of this are complicated -- it's worth reading _MRE_. The short version is that you should almost always replace code that uses $`, $&, or $' with functionally equivalent code that gets the same result without using these variables. For example, this code could be:
if ( /CR List(.*)/ .. /(.*)\(2\)/ ) { if ( /CR List/ ) { print " $1\n"; } elsif ( /\(2\)/ ) { print " $1 \n" ;} else { print " $_\n";} }
...or something to that effect.
Of course, it could be that more recent versions of Perl have fixed this problem and I'm not aware of it. If so, please correct me :-)
-- Chris Devers
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>