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

Hey all, I can't understand the behaviour of my code... Here is my code
$hash = { "s" => "", "d" => "19.28.18.28", "p1" => "", "p2" => "88" }; $s="192.168.3.79"; $d="19.28.18.28"; $p1="3269"; $p2="88"; print $s." -> ".$$hash{"s"}."\n"; if ( $s =~ $$hash{ "s" }) { print "\t1 ok\n";}else { print "\t1 NOK\n" +;} print $d." -> ".$$hash{"d"}."\n"; if ($d =~ $$hash{ "d" }){ print "\t2 ok\n";}else{ print "\t2 NOK\n";} print $p1." -> ".$$hash{"p1"}."\n"; if ($p1 =~ $$hash{ "p1"}){ print "\t3 ok\n";}else{ print "\t3 NOK\n";} print $p2." -> ".$$hash{"p2"}."\n"; if($p2 =~ $$hash{ "p2" }) { print "\t4 ok\n";}else { print "\t4 NOK\n" +;}

Output

192.168.3.79 -> 1 ok 19.28.18.28 -> 19.28.18.28 2 ok 3269 -> 3 NOK 88 -> 88 4 ok

And when I comment the second test (if ($d =~ $$hash{ "d" }){), it prints:

Output

192.168.3.79 -> 1 ok 19.28.18.28 -> 19.28.18.28 3269 -> 3 ok 88 -> 88 4 ok

Why the third test could be one time true and the other time false ?

Thanks

Replies are listed 'Best First'.
Re: Multiple Test Regex Does not work the way I expect
by almut (Canon) on Oct 28, 2009 at 11:38 UTC

    See perlop:

    m/PATTERN/cgimosx (...) If the PATTERN evaluates to the empty string, the last successfull +y matched regular expression is used instead.

    Then, contemplate on what the effects of this (somewhat odd) feature are with your $$hash{"p1"} being empty...

    ___

    P.S.: you'd typically write $s =~ /.../; (i.e. use an explicit pattern delimiter) even though Binding Operators says

    If the right argument is an expression rather than a search patter +n, substitution, or transliteration, it is interpreted as a search pattern at run t +ime.

    P.P.S.: also note that the dots in a pattern like /19.28.18.28/ are regex metacharacters, so maybe you want to use quotemeta  (or rather simply use eq string comparison...?)

      Thanks a lot dude, I added parantheses like they said in the doc you linked at.

      if ($p1 =~ /$$hash{ "p1" }()/)

      Thanks also for the quotemeta, I'll add it too.

Re: Multiple Test Regex Does not work the way I expect
by biohisham (Priest) on Oct 28, 2009 at 14:16 UTC
    You have provided your variables as stringified hash keys as well as test variables and then you're testing their existence against hash values, but $hash{'s'} and $hash{'p1'} values are "empty" still the tests results are not consistent, condsider:
    if ( $s =~ $$hash{ "s" }) { print "\t1 ok\n";}else { print "\t1 NOK\n"; if ($d =~ $$hash{ "p1" }){ print "\t2 ok\n";}else{ print "\t2 NOK\n";} !
    and you get back:
    1 ok #is returned even when the hash doesn't have a corresponding valu +e for $s. 3 NOK #this works correct for $p1?
    Since you have used strings, you better use the string comparison operator "eq" and not the =~, and I bet you got tired of the excessive concatenation, use ' ' around the hash keys instead of " " and you would've been saved the trouble...
    #!/usr/local/bin/perl #title "Multiple Test Regex Does not work the way I expect"; #see the '' around hash keys.. $hash = { "s" => "", "d" => "19.28.18.28", "p1" => "", "p2" => "88" }; $s="192.168.3.79"; $d="19.28.18.28"; $p1="3269"; $p2="88"; print "$s -> $$hash{'s'}\n"; if ( $s eq $$hash{ "s" }) { print "\t1 ok\n"; }else { print "\t1 NOK\n"; } print "$d -> $$hash{'d'}\n"; if ($d eq $$hash{ "d" }){ print "\t2 ok\n"; }else{ print "\t2 NOK\n"; } print "$p1 -> $$hash{'p1'}\n"; if ($p1 eq $$hash{ "p1"}){ print "\t3 ok\n"; }else{ print "\t3 NOK\n"; } print "$p2 -> $$hash{'p2'}\n"; if($p2 eq $$hash{ "p2" }) { print "\t4 ok\n"; }else { print "\t4 NOK\n"; }


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.