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

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: backreference question.
by tilly (Archbishop) on Jun 01, 2004 at 09:20 UTC
    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.

Re: Re: Re: Re: Re: backreference question.
by Qiang (Friar) on Jun 01, 2004 at 17:45 UTC
    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 ) ??