develooper Front page | perl.perl5.porters | Postings from October 2003

Re: [perl #24250] "return" required in some anonymous closures

From:
Dave Mitchell
Date:
October 20, 2003 09:48
Subject:
Re: [perl #24250] "return" required in some anonymous closures
Message ID:
20031020164814.GK2952@fdgroup.com
On Mon, Oct 20, 2003 at 04:59:20PM +0100, Graham Barr wrote:
> It is only supposed to generate a constant sub if there are no other 
> references to the variable. ie there is no way the variable can change.

But it's easy to create a reference after the event:

    sub new_constant_sub {
	my $val = shift;
	my $s = sub() {$val};	# only one ref, so $s a const sub
	$global = \$val;	# whoops
	$s;
    }

> 
> >I always thought you would need to document a bug before it becomes a 
> >feature?  ;-)
> >
> >
> >Seriously, I haven't been able to find _any_ documentation on this 
> >feature.  It definitely is not mentioned in the 5.8.0 perldelta (or it 
> >is described without the terms "constant" or "generator").
> 
> Constant subs were in 5.6.0 IIRC

but anonymous 'const sub generators' were only introduced in 5.8.0, by


[  7389] By: jhi                                   on 2000/10/21  14:26:45
        Log: Subject: Re: Creating const subs for constants.
             From: John Tobey <jtobey@john-edwin-tobey.org>
             Date: Fri, 20 Oct 2000 22:03:27 -0400 (EDT)
             Message-Id: <m13mo0N-000FObC@feynman.localnet>
     Branch: perl
	   ! cv.h embed.h embed.pl objXSUB.h op.c perlapi.c pod/perlapi.pod
	   ! proto.h sv.c

> 
> >If this _is_ a new feature of 5.8.0, maybe it should be at least 
> >documented in the 5.8.2 perldelta?  And possible somewhere else. 
> >Suggestions for locations where this should be documented?  I'll write 
> >up a description and submit a patch then.
> 
> I suspect it is a bug in the code that determines if a sub can be 
> considered a constant sub and was introduced during 5.7 development

No, I think its a deliberate feature (but I could be wrong!)
In particular from that patch:

+
+    if (CvCONST(cv)) {
+       SV* const_sv = op_const_sv(CvSTART(cv), cv);
+       assert(const_sv);
+       /* constant sub () { $x } closing over $x - see lib/constant.pm */
                    ^^^^^^^^^^^^^

-- 
Technology is dominated by two types of people: those who understand what
they do not manage, and those who manage what they do not understand. 



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