Dear brothers and sisters in Perl.

The perlop says:

If the pattern evaluates to the empty string, the last successfully executed regular expression is used instead.

This seems to be true in a simple example:

$ perl -wle '"a" =~ /a/; // and print for qw( a b a b a b )' a a a
The first match is successful, and all the a's that match the same regex are printed.

It behaves correctly when combined with the flip flop operator, too, making it possible to treat the boundary lines specially:

$ perl -le 'print for a .. z' | perl -nle 'if (/d/ .. /h/) { print un +less // }' e f g

It doesn't work as expected when combined with next, though:

$ perl -le 'print for a .. z' | perl -nle 'if (/d/ .. /h/) { next unl +ess //; print }' d f g h

Why is "f" printed?

When combined with -Mre=debug, it shows

... Matching REx "d" against "d" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave 0 Found anchored substr "d" at offset 0 (rx_origin now 0)... (multiline anchor test skipped) Intuit: Successfully guessed: match at offset 0 Matching REx "h" against "d" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "h"... Match rejected by optimizer Matching REx "d" against "d" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave 0 Found anchored substr "d" at offset 0 (rx_origin now 0)... (multiline anchor test skipped) Intuit: Successfully guessed: match at offset 0 Matching REx "h" against "e" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "h"... Match rejected by optimizer Matching REx "d" against "e" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "d"... Match rejected by optimizer Matching REx "h" against "f" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..1] gave -1 Did not find anchored substr "h"... Match rejected by optimizer Matching REx "" against "f" (*) 0 <> <f> | 0| 1:NOTHING(2) 0 <> <f> | 0| 2:END(0) Match successful! ...

Why is there the empty regex (see (*))? Is the magic of // somehow broken by next? Is this the expected behaviour and is it documented anywhere?

Update: When combined with continue, the output changes.

$ perl -le 'print for a .. z' | perl -ne 'if (/d/ .. /h/) { next unle +ss //; print }} continue {' d e f g h
Note that the continue part is empty!

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Empty pattern in regex by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.