Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

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

Playing around with regexes (abusing them :) on the weekend I came across the following (on Perl 5.6.1, ActiveState Build 626). Executing the same regex on the same string in a loop multiple times yields different results for the first run and the remaining runs. I'm running on Win2K, but I don't think this plays a role here. The code:

#!/usr/bin/perl use strict; use warnings; use re qw/eval /; my $pattern = q/(.)(?{ print ++$counts[0]; })^/; my $line = 'ab'; for (0..2) { my @counts = (0); print "$_: "; # $pattern .= '(?=.)'; $line =~ /$pattern/; print "; \@counts = (", join(', ', @counts), ")\n"; } print "\@main::counts = (", join(', ', @::counts), ")\n";

This prints - apart from the warning about the last line:

0: 12; @counts = (2) 1: 34; @counts = (0) 2: 56; @counts = (0) @main::counts = ()
which means, it works the first time as expected but the next times my @counts doesn't get modified by the regex. However, inside the regex the variable seems to retain its value from execution to execution.

When using a package variable by changing my @counts to our @counts the program works as expected and prints:

0: 12; @counts = (2) 1: 12; @counts = (2) 2: 12; @counts = (2) @main::counts = (2)

When uncommenting the $pattern .= line (and going back to my) - effectively changing the pattern in every loop (remark: this does not effect the working of the regex!), the code also works as expected printing:

0: 12; @counts = (2) 1: 12; @counts = (2) 2: 12; @counts = (2) @main::counts = ()

My question - is this a known bug? Is it a bug at all or might I have overlooked a (well) documented feature ;-) and how does this behave in other versions of perl?

-- Hofmator

In reply to Perl Bug in Regex Code Block? by Hofmator

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, 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
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2019-05-22 21:41 GMT
    Find Nodes?
      Voting Booth?
      Do you enjoy 3D movies?

      Results (140 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!