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

The pos() works only outside of matching.

Except when it works inside of matching ;)

$ perl -le'() = "abcdef" =~ /..(?{ print pos() })/g;' 2 4 6

Replies are listed 'Best First'.
Re^5: Using pos() inside regexp (no /e)
by Anonymous Monk on Oct 12, 2010 at 08:05 UTC
    Well, I guess here we talk about 2 different "pos()" functions. The one that is documented under "perldoc -f pos" allows modification of the position where the match has ended. The one you have mentioned, and the one that is used in substitute part of s/// are read-only functions. They can not be used to modify the position. Just the name of the function is the same, but functionality is different.

      There isn't two functions with the same name. (That doesn't even make sense.) It's not read-only.

      s/// uses the specified position when it starts matching (not in the middle), and only for \G. As mentioned by someone else, it makes no sense to modify the pos s/// uses in the middle of its operation.

      If you want to move pos back, you would want to the input with one thing, then replace it again with something else. Replace it with the right thing the first time.

      If you want to move pos forward, s/// has no way to know what you want to do with the chunk you're skipping over. Should it be copied? Should it be replaced?

        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.