Code Comments
Programming Forum and web based access to our favorite programming groups._
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
Post Follow-up to this messageFrom: "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
Post Follow-up to this messagesudhindra 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)")
Post Follow-up to this messageOn 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
Post Follow-up to this messageFrom: 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.