in reply to Re^3: Using pos() inside regexp (no /e)
in thread Using pos() inside regexp

hmm i can read pos() value during matching, so why can't i set it? Sence of this action is deep control of regexp actions.
And about \G, i have no idea how can \G assertion help me to make work regexp from first post:
$data =~s/([^\n]{16})/ch($1)/ge;
Could you, explain how it can help me?

Replies are listed 'Best First'.
Re^5: Using pos() inside regexp (no /e)
by ww (Archbishop) on Oct 09, 2010 at 00:54 UTC
    1. perldoc perlretut (and company)
    2. Tutorials
    3. "Mastering Regular Expressions," Friedl (O'Reilly)

    which is a semi-nice way of saying "RTFM."

    Really!

    Once you learn to fish in Perl's documentation pond, you'll never go hungry.

      I'm sorry, but how that can help me?

      I've read 1 and 3 points of your list, and just take a look at second point. They don't contain answers on my questions, which is:

      1. Is it possible to set pos() value from regexp itself, and how to do it?
      2. How can \G assertion help me to change pos() value?

      Look like you have misunderstand me.
      perldoc -f pos pos SCALAR pos Returns the offset of where the last "m//g" search left + off for the variable in question ($_ is used when the variable +is not specified). Note that 0 is a valid match offset. "und +ef" indicates that the search position is reset (usually du +e to match failure, but can also be because no match has yet + been performed on the scalar). "pos" directly accesses the l +ocation used by the regexp engine to store the offset, so assig +ning to "pos" will change that offset, and so will also influen +ce the "\G" zero-width assertion in regular expressions. Becau +se a failed "m//gc" match doesn't reset the offset, the retu +rn from "pos" won't change either in this case. See perlre and + perlop.
      Point is “"pos" directly accesses the location used by the regexp engine to store the offset” and I want to change it.
      Second point is
      perl -e "\$_='qwerty';s/r/print pos()/e;"
      so I can read pos value, and it is logical that I can set it too. But I don't know how to do it, and can't find any examples.

      Now about \G which says regexp to start next search from the end of previous. It's main purpose to check several regexp on string, special for that its used with c modifier practically all the time. But in my case all actions make in function which called by regexp. But after then function change string, new search must start from earlier position then pos() value. And I don't understand how can \G help me?
      Of course I can miss something, but what?
      I'm sorry for my language level, i can't says clearly what exactly I mean. But hope you will understand.
        I wonder if perhaps you're asking about something like this:
        #!/usr/bin/perl use strict; use warnings; # 864348 $_='qwerty erk'; my $pos; my $foo = $_ =~ s/r/ $pos=pos(). "\n"; print pos(); print "\n"; ++( pos() ); print "\t" . pos() . "\n"; print($pos . "\n")/gex; print "Now, outside the regex, $pos";

        Output:

        3 4 3 8 9 8 Now, outside the regex, 8

        (Note: as you wrote the one-liner in your previous, you wrote an illegal ref to $_; the $ does NOT need escaping in your one-liner.
        ... and I'm confused: "G" and "g" come and go in your posts... sometimes one; sometimes the other; and sometimes, not at all.)