develooper Front page | perl.perl5.porters | Postings from November 2010

Re: [perl #79908] my $x; sub(){$x} makes a constant even if $x changes

Thread Previous
Leon Timmermans
November 29, 2010 14:00
Re: [perl #79908] my $x; sub(){$x} makes a constant even if $x changes
Message ID:
On Mon, Nov 29, 2010 at 9:39 PM, Zefram <> wrote:
> The generation of optimised constant subs is a useful facility.
> It certainly ought to be available, without having to invoke the
> installation-in-a-package behaviour of "use constant".  But this exception
> to the closure rules is a poor way to make it available, since it breaks
> an otherwise logically consistent language feature.  In my opinion,
> constant-sub creation should be made available via an XS function in some
> CPAN module (very easily arranged), and the magic behaviour of sub(){$x}
> should be phased out (less easy, probably requiring a deprecation cycle).

There is no reason constsubs can't close over a variable, they just
wouldn't be very const if they did. The implementation currently
deliberately breaks closures (in op_const_sv it seems), and the
optimizer would make assumptions it shouldn't for such subs, but other
than that it is already supported right now. It may make sense to
explicitly support 'svsubs', which are like constsubs except that they
don't fold to a constant.


Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About