in reply to Misreading m// documentation

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"

Replies are listed 'Best First'.
Re^2: Misreading m// documentation (perlop =~ )
by Anonymous Monk on Mar 12, 2014 at 10:54 UTC

    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
        Sorry, I've been away for a while...

        I pointed you to the wrong node. I replied to a subthread of the OP, and tried to highlight that to several responders, failing. I should have just left it to everyone's own devices to find the additions.

        And it's been so long since I posted last in this thread, I can't remember what I was so excited to tell everyone about. Perhaps just that I initially thought the documentation left something out, but then discovered that it didn't, and could perhaps just be tied together a little tighter.

        Cheers,

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

        And then what happened?
Re^2: Misreading m// documentation (perlop =~ )
by QM (Parson) on Mar 14, 2014 at 17:56 UTC
    =~ 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:

      lanx@nc10-ubuntu:~$ perl -MO=Concise 'abc' =~ '\w{3}' __DATA__ 5 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -:1) v:{ ->3 4 </> match(/"\\w{3}"/) vKS/RTIME ->5 3 <$> const[PV "abc"] s ->4 - syntax OK lanx@nc10-ubuntu:~$ perl -MO=Concise 'abc' =~ m'\w{3}' __DATA__ 5 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -:1) v:{ ->3 4 </> match(/"\\w{3}"/) vKS/RTIME ->5 3 <$> const[PV "abc"] s ->4 - syntax OK

      "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)

        OK, I see the light now.

        <pedantic>
        There's m//, and =~. m// can be used without =~ to match against $_, or with =~ to match against something else.

        Without the presence of =~, m// can omit the m if the delimiter is //, else it is required.

        =~ expects to see a match, substitution, or translation on the RHS, and assumes a match without m//, s//, y//, or tr//.
        </pedantic>

        And since m//, and =~ are 2 separate entities (that slightly overlap), the documentation doesn't tie these together explicitly (IMHO).

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