http://www.perlmonks.org?node_id=412310


in reply to Naked quotes work like m//?

I too have noted that 'bug' as well which as you note turns out not to be one. It is possibly a documentation bug. Probably like me you remembered reading If ``/'' is the delimiter then the initial m is optional in perlre and then made the assumption (IMHO justified) that this implicitly indicates that:

If ``/'' is NOT the delimiter then the initial m is NOT optional.

This of course is an implicit assumption. perlre does not define the behaviour when the delimiter is not a / and the m is missing. As you note the m is optional with a (small) range of other delimiters. This dates back to Perl 5.6.0 and perhaps earlier.

There are actually only a limited number of delimiters that will work in this /* m free, non /* context. Essentially any perl expression that will evaluate as a stringy thing and is a valid RVALUE can be used:

$a = "2foobar\n"; print $a if $a =~ m"foo"; print $a if $a =~ eval{"bar"}; print $a if $a =~ q!foo!; print $a if $a =~ (6*7-40); # yes this evaluates to 2, gets stringif +ied and matches

Practically speaking that means " and ' with most of the other commonly used non / regex delims like ! , | {} all creating syntax errors or not matching.

cheers

tachyon

Replies are listed 'Best First'.
Re^2: Naked quotes work like m//?
by BUU (Prior) on Dec 04, 2004 at 05:09 UTC
    I'm so confused as to what you're going on about here. The code cited in the original post has absolutely nothing to do with "alternate delimiters for m//" and the various other things you're going on about.

    All that is happening is that =~ will use ANY SCALAR on the right hand side as a regex. It stringifies it and treats that value as a regex, then applies it to the left hand as normal.

    So in short, anything that produces a scalar can be used on the right hand side of the =~ operator, including, for example:
    $a =~ { foo }; $b =~ [ "bar" ];
    And so forth. The "m" isn't optional for these because they aren't regexes! They're simply scalar producers.