in reply to what function of this Regular Expression?

***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

Replies are listed 'Best First'.
Re: Re: what function of this Regular Expression?
by BrowserUk (Patriarch) on Apr 02, 2004 at 03:20 UTC
    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

        While your example works as shown it is contrary to the docs (perlop) and also fails to explain the observed behaviour noted above.....
        It's not that there's anything special cased for =~ or double quotes. It's just the general concept of "if you use a thingybob as a fnord, Perl will treat the thingybob as a fnord". Consider the following code, where an array is treated like a number, and a number like a regex.
        $ perl -wle '@a = "foo"; print "Yes" if 2 =~ (@a + 1)' Yes

        Abigail

        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.

        I think that the docs are simply out of date or perhaps somewhat badly phrased. Any expression on the right-hand side of =~ or !~ is treated as a regex.

        print 'String contains a zero' if '0' =~ 0; String contains a zero $_ =~ join '|', 0, 1 and print "'$_' contains a '0' or a '1'" for qw[ 0 2 50 02 1 11 abc1 def]; '0' contains a '0' or a '1' '50' contains a '0' or a '1' '02' contains a '0' or a '1' '1' contains a '0' or a '1' '11' contains a '0' or a '1' 'abc1' contains a '0' or a '1' $_ !~ join '|', 0, 1 and print "'$_' doesn't contains a '0' or a '1'" for qw[ 0 2 50 02 1 11 abc1 def]; '2' doesn't contains a '0' or a '1' 'def' doesn't contains a '0' or a '1'

        The only time the 'm' or '/'s are required is when implicitly comparing against $_.


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