Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Is there any way to rationalize the actual behavior without diving too deeply into the Perl internals?

Its very simple: it's a bug. (In an experimental feature.) Basically the way perls regex engine handles embedded code is subtly wrong in a number of ways. One aspect of this is that you should never use lexicals inside of regexes inside of a repeatable scope (such as the body of a loop or a subroutine). If you are doing a one off it will probably work as you expect, but as soon as you stick the code in a subroutine or something like that and call it twice things dont work out properly. The simple workaround as blokhead explained is to use package level variables and local.

#!perl -l use strict; use warnings; { # BAD for my $i (0..1) { my $count=0; 'abc'=~/.*(?{$count++})(??{'\\z\\A'})/; print "$i:$count"; } } { # Work around #1, single lexical my $count; for my $i (0..1) { $count=0; 'abc'=~/.*(?{$count++})(??{'\\z\\A'})/; print "$i:$count"; } } { # Work around #2, global explicitly named for my $i (0..1) { local $::count=0; 'abc'=~/.*(?{$::count++})(??{'\\z\\A'})/; print "$i:$::count"; } } { # Work around #3, global our $count; # or use vars qw/$count/; for my $i (0..1) { local $count=0; 'abc'=~/.*(?{$count++})(??{'\\z\\A'})/; print "$i:$count"; } }

I beleive dave_the_m has intentions of fixing this one day. But until then pay careful attention to the fact that embedded code is advertised as provisional and experimental which means that you can't really cry too much when it breaks.

---
$world=~s/war/peace/g


In reply to Re^5: Regexes: finding ALL matches (including overlap) (its a bug) by demerphq
in thread Regexes: finding ALL matches (including overlap) by kaif

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (7)
    As of 2014-10-02 11:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (55 votes), past polls