Yep. Adding a print pos() inside the (?{code}) block shows that it does give the right pos values:
sub foo { my $window = "a b X20 c X5 d e X17 X12"; my @o = (); my @m = ($window =~ m/(X\d+(?{print ">pos:",pos();push @o, pos()}) +)/g); print "Matches: @m"; print "Offsets: @o"; print " "; } __END__ >pos:7 >pos:12 >pos:20 >pos:24 Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 >pos:7 >pos:12 >pos:20 >pos:24 Matches: X20 X5 X17 X12 Offsets: >pos:7 >pos:12 >pos:20 >pos:24 Matches: X20 X5 X17 X12 Offsets:

Moving the declaration of @o outside of sub foo makes it clear:

my @o = (); sub foo { my $window = "a b X20 c X5 d e X17 X12"; my @m = ($window =~ m/(X\d+(?{print ">pos:",pos();push @o, pos()}) +)/g); print "Matches: @m"; print "Offsets: @o"; print " "; } __END__ >pos:7 >pos:12 >pos:20 >pos:24 Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 >pos:7 >pos:12 >pos:20 >pos:24 Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 7 12 20 24 >pos:7 >pos:12 >pos:20 >pos:24 Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 7 12 20 24 7 12 20 24

In other words, the @o in the (?{code}) block has been closed over, while the one in the print statement is a fresh array. The closed array is no longer accessible outside the regexp.

A naughty solution might be the following (abusing something of a bug in perl; this may break in future versions):

sub foo { my $window = "a b X20 c X5 d e X17 X12"; my @o = () if pos; # conditional declaration makes @o "static" @o = (); # reset it always my @m = ($window =~ m/(X\d+(?{push @o, pos()}))/g); print "Matches: @m"; print "Offsets: @o"; print " "; } __END__ Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 Matches: X20 X5 X17 X12 Offsets: 7 12 20 24 Matches: X20 X5 X17 X12 Offsets: 7 12 20 24

In reply to Re^2: 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.