From: Chris Devers <[EMAIL PROTECTED]>
> 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 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.

There should be

        if ( /CR List(.*)/ .. /(.*?)\(2\)/ ) {

Otherwise you might get different results.
The original finds the first (2) in the line, your version the last 
one.

> 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 :-)

I believe the penalty is still there.

Of course in a one-off script that contains no other regexps it may 
not matter, but I'd still recommend forgetting these variables even 
exist.

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to