in reply to Re: Re: what function of this Regular Expression?
in thread what function of this Regular Expression?

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

Replies are listed 'Best First'.
Re: what function of this Regular Expression?
by Abigail-II (Bishop) on Apr 02, 2004 at 08:38 UTC
    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

      Hey Abigail, I just love your enlightened way of saying things!

      pelagic
      -------------------------------------
      I can resist anything but temptation.
Re: Re: Re: Re: what function of this Regular Expression?
by BrowserUk (Patriarch) on Apr 02, 2004 at 03:53 UTC

    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