For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > Trying to understand this piece of code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Trying to understand this piece of code
Sudhindra K S

2004-08-03, 9:02 am

_
Hi

Can someone please tell me what the following code is tryingto 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.

Regards
Sudhindra


Jenda Krynicky

2004-08-03, 9:02 am

From: "sudhindra k s" <sudhindra_k_s@rediffmail.com>
> 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 magic is in the .. operator. It returns false until the first
operand evaluates to true, then it returns true until after the
second operand evaluates to true, then again false until the first
becomes true ...

Let me give you a simpler example:

foreach my $var (1,2,3,4,5,6,7,8,9) {
if ($var == 3 .. $var == 6) {
print "$var\n";
}
}


The operator works like the following code:

my $_status = 1;
foreach my $var (1,2,3,4,5,6,7,8,9) {
if ($_status == 1) {
if ($var == 3) {
$_status = 2;
print "$var\n";
}
} else {
print "$var\n";
if ($var == 6) {
$_status = 1;
}
}
}

HTH, Jenda
===== Jenda@Krynicky.cz === 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

Bob Showalter

2004-08-03, 9:02 am

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)")
Chris Devers

2004-08-03, 9:02 am

On Mon, 2 Aug 2004, Bob Showalter wrote:

> sudhindra k s wrote:
>
> 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
Jenda Krynicky

2004-08-03, 9:02 am

From: Chris Devers <cdevers@pobox.com>
> On Mon, 2 Aug 2004, Bob Showalter wrote:
> 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
===== Jenda@Krynicky.cz === 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com