in reply to Re^3: Two hash keys created when '.' is in the string
in thread Two hash keys created when '.' is in the string

"And finally, running any regexp match overwrites the values of all the number variables (for instance, $2), so after checking $2 =~ /css/i, $2 will be undef."

Not true. Perl doesn't garuntee that it will overright those unless they actualy match. Since that regex isn't doing any captures he should be safe, not that I think its the best way to do it, but its safe.


___________
Eric Hodges
  • Comment on Re^4: Two hash keys created when '.' is in the string

Replies are listed 'Best First'.
Re^5: Two hash keys created when '.' is in the string
by ikegami (Patriarch) on Nov 24, 2004 at 19:04 UTC

    "Since that regex isn't doing any captures he should be safe" is wrong. Captures or not, $2 is cleared on a successful match.

    $cfm = 'moo'; 'moo' =~ /()(.*)/; if (($2 =~ /css/i) || ($2 eq $cfm)) { print(defined($2)?1:0, $/); # 1, cause match failed $uniques{$2}++; } 'css' =~ /()(.*)/; if (($2 =~ /css/i) || ($2 eq $cfm)) { print(defined($2)?1:0, $/); # 0, cause match succeeded $uniques{$2}++; # $uniques{undef} ??? }
Re^5: Two hash keys created when '.' is in the string
by Eimi Metamorphoumai (Deacon) on Nov 24, 2004 at 18:55 UTC
    You're right, it only resets them on a successful match, not on an attempted match. But it will overwrite all of them if there's a successful match, even if the match doesn't include any parens. So if the string /css/ matches, $2 will be undef, and in that case the code won't work right.

      /me eats his words.

      You are 100% correct. Here is an example (that I had to run to beleive it).

      my $test = "Hello world!"; print "Matched\n" if $test =~ /(Hello)/; print "\$1 = $1\n"; print "Matched\n" if $test =~ /world/; print "\$1 = $1\n"; __DATA__ Matched $1 = Hello Matched $1 =

      ___________
      Eric Hodges