In both of these examples the string $_ is aliased to the bound string and pos() accurately reflects the current state of the engine. If I modify pos() inside the regex the new value is reflected until the current re_eval exits and then it resets back to it's original value. So what is going on here and what do I need to do to alter the currently bound string's pos() value?

This is the really simple case. I included it so all the instrumentation from the other regex won't clutter things up.

$\ = "\n"; $, = ","; print 'a string' =~ m/(.(?{pos()+=4}))/g; __END__ prints "a, ,s,t,r,i,n,g". It should print "a,i"

Sample B shows this in greater detail

'14567890ab' =~ m/(.(?{$char = substr($_,pos(),1); print "> ".pos()." \"$char\" "; if ($char eq '4' or $char eq '6' or $char eq '8') { print "+".(0+$char)." "; pos() += 0+$char; } else { print '+0 '; } printf "< %2d \"". substr($_,pos(),1)."\" ", pos(); }) (?{printf "-> %2d\n", pos}))+/x; __DATA__ > 1 "4" +4 < 5 "8" -> 1 # alter pos() > 2 "5" +0 < 2 "5" -> 2 > 3 "6" +6 < 9 "b" -> 3 # alter pos() > 4 "7" +0 < 4 "7" -> 4 > 5 "8" +8 < 10 "" -> 5 # alter pos() > 6 "9" +0 < 6 "9" -> 6 > 7 "0" +0 < 7 "0" -> 7 > 8 "a" +0 < 8 "a" -> 8 > 9 "b" +0 < 9 "b" -> 9 > 10 "" +0 < 10 "" -> 10
__SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

In reply to immutable pos() inside regex by diotalevi

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.