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

I did some testing on my Perl 5.10 Win XP system. At first all appeared to be ok. Then I tried to replicate the output of the poster and found that I could do it if the second match failed.

It appears that if match fails, $1 remains the same as it was, i.e. $1 is only valid if match succeeds.

I experimented with  ${type} vs just $type and found that both worked on my Perl version. I am wondering if something about this on Perl 5.8.0 is somehow different? That this second match is not succeeding and old $1 is still there? I would suggest trying some of my experiments below and see what happens on the OP's system. I think the second match is failing for some reason and the "old $1" is getting printed.

#!/usr/bin/perl -w print "get some string: "; ($string = <STDIN>); #note: chomp not necessary, $ should count \n as #end of string. should work with or without chomp. $string =~ m/\/([[:alnum:]]+)_.*\.(.+)$/; print "dollar 1: $1\n"; $type = $2; print "type: $type\n"; #something weird here.... $string =~ m/(.+)\.BBB$/; #get some string: /xxxx/yyyy/ZZZ_xxxx.CCC #dollar 1: ZZZ #type: CCC #dollar 1:ZZZ print "match failed\n" unless $string =~ m/(.+)\.BBB$/; #$string =~ m/(.+)\.${type}$/; #ok #$string =~ m/(.+)\.$type$/; #ok also print "dollar 1:$1\n"; exit 0; __END__ This is with the match failed code: C:\TEMP>regextest.pl get some string: /xxxx/yyyy/ZZZ_xxxx.CCC dollar 1: ZZZ type: CCC match failed dollar 1:ZZZ This is from: #$string =~ m/(.+)\.${type}$/; #ok #$string =~ m/(.+)\.$type$/; #ok also C:\TEMP>regextest.pl get some string: /xxxx/yyyy/ZZZ_xxxx.CCC dollar 1: ZZZ type: CCC dollar 1:/xxxx/yyyy/ZZZ_xxxx C:\TEMP>perl -v This is perl, v5.10.0 built for MSWin32-x86-multi-thread (with 5 registered patches, see perl -V for more detail)