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

I understand your arguments. But the point is simple. Using "pos() = $new_pos" does not work during the matching process. So either the "perldoc -f pos" has to be updated, or the function available during matching should be renamed. Of course, more appropriate is to update the description of "pos" function. This way we wouldn't need to argue about what makes sense and what does not. Everything would be nice and clear. The update of the last match position is possible only outside of matching. This is exactly what I meant in my first message.
  • Comment on Re^7: Using pos() inside regexp (no /e)

Replies are listed 'Best First'.
Re^8: Using pos() inside regexp (no /e)
by ikegami (Patriarch) on Oct 13, 2010 at 16:02 UTC

    Using "pos() = $new_pos" does not work during the matching process.

    That is also wrong. pos works in a substitution expression just like it does outside a substitution expression. Had you checked, you would have seen that lvalue pos does in fact change the var's pos when used in the substitution expression.

    $ perl -E'my $x="abc"; $x=~s/./say pos($x); pos($x)=2; say pos($x)/e;' 0 2

    s/// simply doesn't care that you changed it. Like I've already explained, it simply doesn't make any sense for s/// to check if the string's pos has changed. Why are you still asking about this?

      What's the difference? The change does not have any effect on the matching. So it is the same as not working at all. After your matching ends, the set value won't even be preserved. So, for the original question "Can pos() be used inside of matching to change where the \G matches" the answer is "No. The pos does not work during matching. In other words, it does not affect the position where the \G matches". And this should be written in the documentation.

        So, for the original question "Can pos() be used inside of matching to change where the \G matches" the answer is "No. The pos does not work during matching.

        \G can only possibly match where you left off. (Not processing part of the input string or processing part of the input string twice makes no sense.) Since \G can't possibly match elsewhere, trying to make it do so would result in a pattern that would never match.

        So no, the answer is "No, it makes no sense".

        The change does not have any effect on the matching. So it is the same as not working at all.

        No, it means you think s/// or \G is broken, not pos(). But s/// and \G aren't broken.