in reply to Naked quotes work like m//?

Perl has been able to use plain strings as regexes for as long as I know. Usually that is intended to be used as
$pat = 'fo.d'; $s =~ $pat
but what you got here is just a weird case of pretty much the same, and as such, works too, for some meaning of "work".

Do note that using // or "" as delimiters is not the same thing. One's a regex, the other a string. Oh, and using m with quotes as delimiter, makes it a regex again. Witness the difference:

#! perl -w $\ = "\n"; # one line per print $s = "abc\\.def"; print $s; $s =~ /(\\.)/ and print $1; $s =~ "(\\.)" and print $1; $s =~ m"(\\.)" and print $1;
resulting in:
abc\.def
\.
.
\.

Replies are listed 'Best First'.
Re^2: Naked quotes work like m//?
by Nkuvu (Priest) on Dec 05, 2004 at 19:03 UTC
    A bit confused here. The string "(\\.)" isn't the same as the regexen in the other two examples. Because stringification removes a level of backslashes, so the regex applied in the second example is really (\.) which is, of course, very different from (\\.)

    I think the backslash obscures the issue a lot, though, so I added another test without any backslashes: $s =~ "(a.c)" and print $1;

    It produced abc which seems to me to indicate that the string is actually being evaluated as a regex. And I think you're saying that this isn't the case.

    So what am I missing? Or misinterpreting?

      What you're missing is that the value of the string is interpreted as the regex, and not how you write the source code for the string. So a double backslash in the source code is actually just a single backslash in the string, and in the regex; and 4 backslashes in the source code is actually a double backslash in both string and regex.
        No, I understood that part. I just thought that you were saying that a string isn't applied like a regex at all -- so it seems that this is what I misinterpreted. Thanks for the explanation.