Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Misreading m// documentation

by QM (Parson)
on Mar 12, 2014 at 10:23 UTC ( [id://1077990]=perlquestion: print w/replies, xml ) Need Help??

QM has asked for the wisdom of the Perl Monks concerning the following question:

A colleague used this today, and it worked as a match:
$x =~ '^something';

I thought the delimiter had to be /, or the leading m was required. From Regexp Quote Like Operators:

If "/" is the delimiter then the initial m is optional.
Given my shortsightedness today, can someone point me to the relevant line in the docs?

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re: Misreading m// documentation
by LanX (Saint) on Mar 12, 2014 at 11:05 UTC
    Its the binding operator =~ which tells you that the RHS is a regex.

    The m is optional, if its clearly supposed to be a regex.

    IIRC are single quoted regexes documented to be taken literally, i.e. w/o variable interpolation.

    edit

    Indeed, see perlop

    If "'" is the delimiter, no interpolation is performed on the PATTERN. 

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      > If "'" is the delimiter, no interpolation is performed on the PATTERN.

      hmm, strange in 5.10 this uncommon feature seems to be buggy:

      $ perl if ( '_$x_' =~ '$x') { print "match 1" } if ( '_$x_' =~ /\$x/){ print "match 2" } __DATA__ match 2

      while B::Deparse claims both lines to be identical ...

      perl -MO=Deparse if ( '_$x_' =~ '$x') { print "match 1" } if ( '_$x_' =~ /\$x/){ print "match 2" } __DATA__ if ('_$x_' =~ /\$x/) { print 'match 1'; } if ('_$x_' =~ /\$x/) { print 'match 2'; } __DATA__ - syntax OK

      ... does B::Concise show that the interpolation is not inhibited

      perl -MO=Concise if ( '_$x_' =~ '$x') { print "match 1" } if ( '_$x_' =~ /\$x/){ print "match 2" } __DATA__ g <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 3 -:1) v:{ ->3 - <1> null vKP/1 ->9 5 <|> and(other->6) vK/1 ->9 4 </> match(/"$x"/) sKS/RTIME ->5 <-- INTERPOLA +TED 3 <$> const[PV "_$x_"] s ->4 - <@> scope vK ->- - <0> ex-nextstate v ->6 8 <@> print vK ->9 6 <0> pushmark s ->7 7 <$> const[PV "match 1"] s ->8 9 <;> nextstate(main 5 -:2) v:{ ->a - <1> null vKP/1 ->g c <|> and(other->d) vK/1 ->g b </> match(/"\\$x"/) sKS/RTIME ->c <-- NO INTERP +OLATION a <$> const[PV "_$x_"] s ->b - <@> scope vK ->- - <0> ex-nextstate v ->d f <@> print vK ->g d <0> pushmark s ->e e <$> const[PV "match 2"] s ->f - syntax OK

      added marks to highlight differences.

      Can someone please check for other perl versions?

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Bug in Deparse. Still present.

        The regex pattern is

        $x # End of line followed by "x"

        so the literal should be

        m'$x'

        It's the only match operator that will produce exactly that regex pattern.

        shorter example

        interpolation, the \Q\E is interpolated in double quoted strings

        $ perl -Mre=debug -e " $f=q{a??q}; $f =~ m{\Qa??\E}; " Compiling REx "a\?\?" Final program: 1: EXACT <a??> (3) 3: END (0) anchored "a??" at 0 (checking anchored isall) minlen 3 Guessing start of match in sv for REx "a\?\?" against "a??q" Found anchored substr "a??" at offset 0... Guessed: match at offset 0 Freeing REx: "a\?\?"

        use qr to see how \Q\E gets interpolated

        $ perl -le " print qr{\Qa??\E}; " (?^:a\?\?)

        without interpolation the pattern is completely different

        $ perl -le " print qr'\Qa??\E'; " (?^:\Qa??\E)

        The regex metacharcters are still regex metacharacters (pattern)

        $ perl -Mre=debug -e " $f=q{a??q}; $f =~ m'\Qa??\E'; " Compiling REx "\Qa??\E" Final program: 1: EXACT <Q> (3) 3: MINMOD (4) 4: CURLY {0,1} (8) 6: EXACT <a> (0) 8: EXACT <E> (10) 10: END (0) anchored "Q" at 0 floating "E" at 1..2 (checking floating) minlen 2 Guessing start of match in sv for REx "\Qa??\E" against "a??q" Did not find floating substr "E"... Match rejected by optimizer Freeing REx: "\Qa??\E"

        perlrebackslash :)

        Um, $ is still a metacharacter, try '\$x'
Re: Misreading m// documentation
by AnomalousMonk (Archbishop) on Mar 12, 2014 at 14:34 UTC
    If "/" is the delimiter then the initial m is optional.

    I think this statement in Regexp Quote-Like Operators (in the discussion of  m//) is intended, albeit without the best clarity, to refer to the case of implicit matching against $_. In this case, the statements  /foo/; or  m/foo/; or  m{foo}; or  m'foo'; will all operate as intended, but the statements  {foo}; or  'foo'; will obviously not: they are simply null statements.

Re: Misreading m// documentation (perlop =~ )
by Anonymous Monk on Mar 12, 2014 at 10:48 UTC

    see =~ in perlop

    =~ with a string implies regex m//atch

    $ perl -Mre=debug -e " $_=q{shabba}; $_ =~ q{shabba} Compiling REx "shabba" Final program: 1: EXACT <shabba> (4) 4: END (0) anchored "shabba" at 0 (checking anchored isall) minlen 6 Guessing start of match in sv for REx "shabba" against "shabba" Found anchored substr "shabba" at offset 0... Guessed: match at offset 0 Freeing REx: "shabba"

      PPI gets it right, its a string ... or is that "wrong" of ppi? ha

      $ ppi_dumper -W shabba PPI::Document PPI::Statement PPI::Token::Symbol '$x' PPI::Token::Operator '=~' PPI::Token::Quote::Single ''^something'' PPI::Token::Structure ';'

        My attention was directed to the post to which this is a reply. I'm not quite sure what the point of that post is, or why it was brought to my attention. All I can say is: Of course '^something' is a single-quoted string literal.

        That doesn't mean it the code doesn't result in a regex match. We already know it does.

        >perl -MO=Concise,-exec -e"$x =~ '^something'" 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <#> gvsv[*x] s 4 </> match(/"^something"/) vKS/RTIME 5 <@> leave[1 ref] vKP/REFC -e syntax OK
      =~ with a string implies regex m//atch
      Documented in doc:/perlop#Binding-Operators:
      If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        > If the right argument is an expression rather than a search pattern,

        True, but this doesn't apply here, it's literal a search pattern (i.e. compile time), the m is just optional:

        Both generate exactly the same opcodes:

        "expressions evaluated at runtime" are things like variables or functions or do-blocks...

        DB<103> $x= '\w{3}' => "\\w{3}" DB<104> 'abc' =~ $x => 1 DB<105> sub regex { '\w{3}' } DB<106> 'abc' =~ regex() => 1

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Misreading m// documentation
by jimmy.tty (Scribe) on Apr 01, 2014 at 03:26 UTC

    Looks like the same behavior or split builting function:

    perldoc -f split

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1077990]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found