Glad you're going to avoid that "feature" :-) (see e.g. Re: static-like persistence of my variable due to trailing conditional and How does my work with a trailing conditional for previous discussions)

I'd like to give you two reasonable workarounds. The first is simply using the global variable, with my added suggestion to enclose it in an anonymous block:

{ # limit scope my @o; sub foo { my $window = "a b X20 c X5 d e X17 X12"; @o = (); my @m = ( $window =~ m/(X\d+(?{push @o, pos()}))/g ); print "Matches: @m"; print "Offsets: @o"; print " "; } }
This will make sure that only foo() can see @o.

The second workaround is basically a rewrite of your code. It doesn't solve the general issue, but it avoids the use of the complicated (?{BLOCK}) feature for your particular case:

sub foo { my $window = "a b X20 c X5 d e X17 X12"; my( @o, @m ); while( $window =~ m/(X\d+)/g ) { push @m, $1; push @o, $+[0]; } print "Matches: @m"; print "Offsets: @o"; print " "; }

In reply to Re^4: Multiple uses of (?{ code }) do not appear to be called by rhesa
in thread Multiple uses of (?{ code }) do not appear to be called by bsdz

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.