http://www.perlmonks.org?node_id=505696

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

Hello monks,

I have the following subroutine which I use with sort:
my $mask=/_(\d\d)_/; sub sorter ($$){ my ($am,$bm); $_[0] =~ /$mask/; $am=$1; '' =~ /()/; # A match that always succeeds $_[1] =~ /$mask/; $bm=$1; $am <=> $bm || $am cmp $bm; } @result = sort sorter @f;
The idea is to sort a list of strings based on part of the string. So I use two matches for each string passed on sorter function, but if the first match succeeds and the seconds doesn't the $1 variable is still holding the result of the fist match as if the second match never happened. If both matches are successfull everything is working as expected but if the first one matches and the second don't the second match dont affect the backreferences in $1. I found a wrokaraoud for this by placing an always succeeding empty match between the matches but I wonder is this normal behaviour?

Can anyone explain it?

Zaro