in reply to backreference question.

Given davidj's demonstration, the question becomes: what sort of object is $resp? It's plausible that putting the object's "content" method in a while loop has the effect of resetting the regex engine's pointer back to the beginning of the string on each iteration.

update: if you put the value of $resp->content into a scalar, you should be able to put that scalar into the while loop and have it work as expected -- e.g.:

my $string = $resp->content; while( $string=~/your_regex/g ) { ... }

Replies are listed 'Best First'.
Re: Re: backreference question.
by Qiang (Friar) on Jun 01, 2004 at 01:58 UTC
    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");
    
      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.