Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

messing up $line?

by perlNinny (Beadle)
on Apr 27, 2007 at 17:13 UTC ( #612421=perlquestion: print w/replies, xml ) Need Help??

perlNinny has asked for the wisdom of the Perl Monks concerning the following question:

Let's say I have this
my $line = $_; if($line =~ s/^.+\/\.(.*)$/$1/) { print ". extension ".$line."\n"; } elsif ( $line =~ s/^.+\.(.*)$/$1/) { print "Extension ".$line."\n"; }
Now then, does $line end up altered after the first if so that the 2nd if is less effective?

Replies are listed 'Best First'.
Re: messing up $line?
by chromatic (Archbishop) on Apr 27, 2007 at 17:19 UTC

    No; the conditions are exclusive.

      So line doesn't change because the first check is more limited than the second and the substitution isn't fired if it doesn't match. COOL
Re: messing up $line?
by jettero (Monsignor) on Apr 27, 2007 at 17:17 UTC
    I would say the second if (er, elsif in this case) isn't going to fire if the first if actually does a replacement. So no...

    If they were separate if statements, then yes (possibly). I don't believe the string is ever altered unless an actual replacement happens.

    -Paul

Re: messing up $line?
by johngg (Canon) on Apr 27, 2007 at 20:36 UTC
    Minor code point, to save yourself some typing take advantage of variable interpolation inside double-quoted strings.

    my $line = $_; if($line =~ s/^.+\/\.(.*)$/$1/) { print ". extension $line\n"; } elsif ( $line =~ s/^.+\.(.*)$/$1/) { print "Extension $line\n"; }

    Note that no interpolation takes place inside single-quoted strings.

    Cheers,

    JohnGG

Re: messing up $line?
by cengineer (Pilgrim) on Apr 27, 2007 at 17:18 UTC
    You're just testing for a match. $line is not being altered.
      I take that back. I missed the s/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://612421]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-08-07 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?