Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: perl pattern match for end of string using STDIN and chomp

by AnomalousMonk (Archbishop)
on Oct 08, 2009 at 21:48 UTC ( [id://800105]=note: print w/replies, xml ) Need Help??


in reply to perl pattern match for end of string using STDIN and chomp

I tend to agree with the suggestions of others that the second regex in the chomped <STDIN> section of the OPed code is simply failing to match, and the previous value of  $1 is persisting.

Try inserting the statement
    print qq{\$1 reset: '$1'} if 'foo' =~ /(foo)/;
between the first and second regex in that section and seeing what happens. If the second  'ZZZ' becomes  'foo' you will know what is happening (although not why the second regex fails to match, which I cannot understand myself).

Replies are listed 'Best First'.
Re^2: perl pattern match for end of string using STDIN and chomp
by Marshall (Canon) on Oct 08, 2009 at 23:43 UTC
    This "previous value of $1" can be problematic. One of the practices that I often use in my code is to NOT use $1 or $2, etc. I like to assign $1 right away to a variable that has more contextual meaning (like $name, $cust_id) or whatever. Why have $name = $1;?

    One way to do this is illustrated below, put the match into a list context and use list slice to get $1,$2, etc. If "$1" is undef, then in this case $thing gets undef, not the previous value of $1.

    print "match failed\n" unless $string =~ m/(.+)\.BBB$/; print "dollar $1:\n"; #prints previous $1 value my ($thing) = ($string =~ m/(.+)\.BBB$/)[0]; print "thing =$thing\n"; #$thing is undef
Re^2: perl pattern match for end of string using STDIN and chomp
by cbolcato (Novice) on Oct 09, 2009 at 12:28 UTC
    it seems pretty clear that the $1 in the code is falling through since the second regex is not matching, so i will use some of the code examples to avoid that in the future. However, the big question is why the second regex does not match with a chomped <STDIN> but will match a chomped $ARGV[0], it only appears to be an issue with 5.8.0, and appears to have been fixed in atleast 5.8.3, can anyone explain this behavior, may need some ammo to get our Linux support to upgrade our standard Perl version

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-09-18 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (23 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.