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

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

From:
Graham Barr
Date:
October 20, 2003 10:20
Subject:
Re: [perl #24250] "return" required in some anonymous closures
Message ID:
AA961E92-0321-11D8-8802-0003938857CC@pobox.com

On 20 Oct 2003, at 17:48, Dave Mitchell wrote:

> 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

But that is not when its decided that its a constant sub. Or at least 
originally it was not.

When constant subs were first introduced it was in the optimizer that 
the decision was taken if a sub was constant, not when the sab was 
made. The decision was "does the sub being called have a proto of () 
and is its body a single scalar with a refcnt of 1", if yes then inline 
it.

I am guessing that logic has been pushed back to when the sub is 
defined, which has resulted in this bug

Graham.

> 	$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