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 06:57
Subject:
Re: [perl #24250] "return" required in some anonymous closures
Message ID:
20031020135700.GE2952@fdgroup.com
On Mon, Oct 20, 2003 at 02:29:11PM +0200, Elizabeth Mattijsen wrote:
> At 11:45 +0100 10/20/03, Dave Mitchell wrote:
> >On Mon, Oct 20, 2003 at 12:17:24PM +0200, Elizabeth Mattijsen wrote:
> >> (I should stop answering my own posts  ;-)
> >>
> >> At 11:55 +0200 10/20/03, Elizabeth Mattijsen wrote:
> >> >Hmmm... both of the following return correct results also:
> >>
> > > The problem can be simplified to the following code:
> > > so this looks like an optimizer problem to me: the $get sub is
> > > referring to something else than the package lexical $foo.
> >This isn't a bug, it's a feature :-)
> >sub () {$lexical} is a constant sub generator, ie each call to 'sub'
> >returns a new constant sub rather than a closure. It doesn't seem to be
> >well documented though. Been around since 5.8.0.
> 
> 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").
> 
> 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 think the best place would be in perlsub.pod in
=head2 Constant Functions

Personally I think its a confusing feature, but it appears to be used
in lib/constant.pm - presumably as a cheap way of creating the const subs
as needed. C<use constant foo => 1> gets implemented like
    { my ($name, $val) = @_; *$name = sub(){$val} }

Anyone feel like deprecating it ;-)

-- 
Little fly, thy summer's play my thoughtless hand has
terminated with extreme prejudice.
        (with apologies to William Blake)



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