develooper Front page | perl.perl5.porters | Postings from January 2013

Re: [perl #116577] multicall breaks lexicality of variables

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
January 30, 2013 19:06
Subject:
Re: [perl #116577] multicall breaks lexicality of variables
Message ID:
20130130190550.GS2066@iabyn.com
On Tue, Jan 29, 2013 at 11:46:25AM -0800, Zefram wrote:
> $ cat t0.pl
> use List::Util qw(reduce);
> our @q;
> sub ggg {
> 	my $x = $a;
> 	push @q, \$x;
> 	1;
> }
> sub fff { reduce \&ggg, 1, 2, 3; }
> fff();
> fff();
> print $_,"\n" for @q;
> $ perl t0.pl
> SCALAR(0x1f52fc0)
> SCALAR(0x1f52fc0)
> SCALAR(0x1f53128)
> SCALAR(0x1f53128)
> $
> 
> ggg() is only getting one $x variable per invocation of reduce(),
> whereas it should get one per invocation of ggg().  This seems to be
> specific to the multicall mechanism (of which reduce() is the exemplar).

Looks like MULTICALL doesn't do a SAVE/LEAVE or equivalent around each
function call: so all the clearing up (of lexical vars, local() etc)
onky happens at the end of the reduce, in POP_MULTICALL.

I guess for correctness-over-performance's sake it should (although
it will need to special-case calling /(?{})/, which is documented
as accumulating local()s).

I note that sort(), which inspired MULTICALL, *does* pop the save stack
after each call.

-- 
You're only as old as you look.

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