[pm-h] Discussion about delimiters for m//

Reini Urban reini.urban at gmail.com
Tue Mar 25 14:30:55 PDT 2014


On Feb 14, 2014, at 5:55 PM, G. Wade Johnson <gwadej at anomaly.org> wrote:

> Near the end of the meeting last night, there was a minor discussion at
> the whiteboard about which characters Perl allows as delimiters on the
> match operator.
> 
> According to PerlDoc:
> 
> If "/" is the delimiter then the initial "m" is optional.  With the
> "m" you can use any pair of non- whitespace (ASCII) characters as
> delimiters.  This is particularly useful for matching path names that
> contain "/", to avoid LTS (leaning toothpick syndrome).  If "?" is the
> delimiter, then a match-only- once rule applies, described in
> "m?PATTERN?" below.  If "'" is the delimiter, no interpolation is
> performed on the PATTERN.  When using a character valid in an
> identifier, whitespace is required after the "m".
> 
> So, that means that 
> 
>  $str =~ m mabc+m;
> 
> is valid Perl and it matches "abc+".
> 
> Weird. I can't think of a real use for it and using it in production
> code is likely to get you beaten by whoever maintains your code.

Me neither.

This is how to test it, with a debugging perl

$ perl -Dr -e'print "abc" =~ m mabc+m;'
Compiling REx "abc+"
rarest char c at 0
rarest char b at 1
Final program:
  1: EXACT <ab> (3)
  3: PLUS (6)
  4:   EXACT <c> (0)
  6: END (0)
anchored "abc" at 0 floating "c" at 2..2147483647 (checking anchored) minlen 3
Omitting $` $& $' support.

EXECUTING...

Guessing start of match in sv for REx "abc+" against "abc"
Found anchored substr "abc" at offset 0...
Found floating substr "c" at offset 2...
Guessed: match at offset 0
Matching REx "abc+" against "abc"
  0 <> <abc>                |  1:EXACT <ab>(3)
  2 <ab> <c>                |  3:PLUS(6)
                                 EXACT <c> can match 1 times out of 2147483647...
  3 <abc> <>                |  6:  END(0)
Match successful!
1Freeing REx: "abc+"

And B::Concise shows you the match operator and flags being used:
=> /abc+/

$ perl -MO=Concise -e'print "abc" =~ m mabc+m;'
7  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
6     <@> print vK ->7
3        <0> pushmark s ->4
5        </> match(/"abc+"/) lKS/RTIME ->6
4           <$> const(PV "abc") s ->5
-e syntax OK



More information about the Houston mailing list