Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Naked quotes work like m//?

by tachyon (Chancellor)
on Dec 03, 2004 at 23:25 UTC ( #412310=note: print w/replies, xml ) Need Help??

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.



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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://412310]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2020-05-30 19:15 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (173 votes). Check out past polls.