in reply to Re: backreference question.
in thread backreference question.

It's plausible that putting the object's "content" method in a while l +oop has the effect of <br>resetting the regex engine's pointer back t +o the beginning of the string on each iteration.
thank you, graff , that's probably the cause of the problem. I haven't found much on that topic yet. but putting the $resp->content into a scalar before the while loop solve the problem.

btw, the $resp is a response object from LWP module,like this

 $br = LWP::UserAgent->new;
 $resp = $br->get("http://www.example.com");

Replies are listed 'Best First'.
Re: Re: Re: backreference question.
by tilly (Archbishop) on Jun 01, 2004 at 07:24 UTC
    There are many possibilities for why what happened happened. None are likely to be heavily documented.

    If the content method returns a different Perl scalar each time with the same contents, then what happens is that each pattern match is matching a scalar that has never been matched before. So each time Perl will start from the beginning.

    If the content method attempts to do a pattern match internally, then pos is being reset.

    If it directly assigns to pos (I can't imagine why it would but...), then that assignment wins.

    In any case few module authors would think about someone trying to do a pattern match like this, so it is a good idea not to expect it to be documented accurately, and not to rely on them making your life easy. Putting the content into a scalar like graff suggested is a generally good idea unless you really know the code you are calling, and how Perl will deal with that.

      This code
      sub resp { "abcdefg" } while (resp =~ /(.)/g) { print $1 }
      goes into an infinite loop. No internal setting of pos, no pattern matching, whatever, same value. I can get the while loop to work(?) by making resp return a reference to a global scalar, and deref it in the while conditional with ${resp()}, but what module author would think about that?

      Having written this, I'm not entirely sure what my point was, but I'm sure I had one.
        Whether or not you knew your point, you had a good one and I just learned something that I should have known. ;-)

        Perl calls by reference, returns by value. The "return by value" semantic means that the returned scalar is different - at least different enough to forget any pos information. Which means that it would be difficult to impossible for one to design a function that could be called in a loop like this without causing a problem.

        did you mean like this when you say ref/deref :

        sub resp { $a="abcdefg";\$a } while (${&resp} =~ /(.)/g) { print $1; }
        however that goes to an infinitely loop too and for the first time on windows I crash perl interpreter so fast (when i hit Ctrl^C).
        sub resp { "abcdefg" } while (&resp =~ /(.)/g) { # you forgot the &,didn't you? print $1; }
        in the above code, I expect that &resp returns the 'value' of the subroutine and becomes
        while ("abcdefg" =~ /(.)/g) { print $1; }
        it would work if it were the case.

        so return by ref doesn't work either. what happens in while( &resp =~ /(.)/g ) ??