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

foreach (keys %flows) { if (!($_ =~ "^0\$")) {

I don't know what's the function of the regular expression?

can you please explain it ?

thanks!

Replies are listed 'Best First'.
Re: what function of this Regular Expression?
by tachyon (Chancellor) on Apr 02, 2004 at 03:03 UTC

    ***This is not an RE***. You need m"this" syntax to make it an RE. The only time you can not use m is if you have syntax $foo =~ /this/ Using the / char as the RE delimiter makes the m implicit. Your 'RE' uses " as the delimiter so needs the m. It does not do what I expect the original programmer thought it would. Here is what it does:

    use warnings; my %flows = ( '0' => 1, '00' => 2, '0$' => 3, '0$more' => 4, 'foo0$' = +> 5 ); foreach (keys %flows) { # loop through keys of %flows setting $_ to each key in turn if (!($_ =~ "^0\$")) { print "Orig $_ not matched\n"; } else { print "Orig $_ matched\n"; } if (!($_ =~ m"^0\$")) { print "RE $_ not matched\n"; } else { print "RE $_ matched\n"; } print $/; } __DATA__ Orig 0 matched RE 0 not matched Orig 00 not matched RE 00 not matched Orig 0$more not matched RE 0$more matched Orig 0$ not matched RE 0$ matched Orig foo0$ not matched RE foo0$ not matched

    I am intrigued it is not a syntax error or warning. It appears that the intent is to do something if the hash key does not start with the 2 literal chars '0$'. It does not do that as you can see.

    cheers

    tachyon

      You need m"this" syntax to make it an RE

      Actually, not.

      print "Found '$1'" if 'one two three' =~ 'one(.*)three'; Found ' two '

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail

        While your example works as shown it is contrary to the docs (perlop) and also fails to explain the observed behaviour noted above.....

        If ``/'' is the delimiter then the initial m is optional. With the m y +ou can use any pair of non-alphanumeric, non-whitespace characters as + delimiters. C:\>type test.pl $re =~ "^0\$"; $re =~ m"^0\$"; $re =~ '^0\$'; $re =~ m'^0\$'; $re =~ <^0\$>; $re =~ m<^0\$>; C:\>perl -MO=Deparse test.pl $re =~ /^0$/; $re =~ /^0\$/; $re =~ /^0\$/; $re =~ /^0\$/; $re =~ /CORE::GLOBAL::glob('^0$', 0)/; $re =~ /^0\$/; test.pl syntax OK C:\>

        The quote and other chars can also be used without m but the results may not be intuitive.....

        cheers

        tachyon

Re: what function of this Regular Expression?
by iwanthome (Beadle) on Apr 02, 2004 at 06:58 UTC
    thanks everybody who give me advice.

    To BrowserUk ,I don't understand how to get interpolated as string when it is "^0\$".Can you explain more detail? thanks!

    The reason for the observed behaviour is that a regex with "s as the delimiter get interpolated as a string, before it gets treated as a regex.

    my test script like this:

    $_ = '0$'; if($_ =~ "^0\$") { print "find\n"; } else { print "not find\n"; } result is: not find
    why regex can't match ? It looks like tachyon said "***This is not an RE***. " thanks!