develooper Front page | perl.perl5.porters | Postings from December 2002

Re: [perl #19236] perlsyn: implicit localisation in while()

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
December 26, 2002 10:02
Subject:
Re: [perl #19236] perlsyn: implicit localisation in while()
Message ID:
20021226173628.GC284@Bagpuss.unfortu.net
On Tue, Dec 24, 2002 at 01:20:06PM +1100, Martien Verbruggen wrote:

> If the block did the localisation, then the line starting with 4: would
> read the same as the line starting with 2:, since $1 gets set in the
> conditional of the while() loop, not inside the block. To see what I
> mean, try this:

> or even this:
> 
> $ perl -wl
> $_ = "ab";
> /(.)$/;
> print "1: $1";
> for (/(.+)/g)
> {
>     print "2: $1";
>     /(.)/;
>     print "3: $1";
> }
> print "4: $1";
> __END__
> 1: b
> 2: ab
> 3: a
> 4: ab
> 
> In neither of these two cases the localisation occurs. It only occurs
> when the while() is used.


> But I merely wanted to draw attention to the discrepancy in
> documentation and behaviour. I don't _know_ what is correct and what
> isn't, since that is the domain of the p5p to discuss.

There does seem to be something astray, as these two aren't equivalent:

$ perl -wl
$_ = "ab";
/(.)$/;
print "1: $1";
while (/(.+)/g) {
}
print "4: $1";
__END__
1: b
4: b

$ perl -wl
$_ = "ab";
/(.)$/;
print "1: $1";
1 while (/(.+)/g);
print "4: $1";
__END__
1: b
4: ab


They don't seem to compile to the same ops - the former's loop is:
    COP (0x16d828) nextstate
    BINOP (0x16d800) leaveloop
        LOOP (0x16d7d0) enterloop
        UNOP (0x16d7b0) null
            LOGOP (0x16d788) and
                PMOP (0x16d4e8) match /(.+)/
                LISTOP (0x16d760) lineseq
                    COP (0x16d6c0) nextstate
                    OP (0x16d700) unstack
                    COP (0x16d720) nextstate

the latter is:
    COP (0x16d7b8) nextstate
    LISTOP (0x16d790) leave
        OP (0x16d770) enter
        UNOP (0x16d750) null
            LOGOP (0x16d728) and
                PMOP (0x16d508) match /(.+)/
                LISTOP (0x16d700) lineseq
                    OP (0x16d4d8) null [5]
                    OP (0x16d6e0) unstack

I don't know what the significance of all this is. The for loop compiles
to a third set of ops. I get the impression that the while loop's condition
is scoped within the block, whatever the documentation may say.

Nicholas Clark

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About