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

Re: [perl #79908] my $x; sub(){$x} makes a constant evenif$xchanges

Thread Previous | Thread Next
From:
Dr.Ruud
Date:
December 2, 2010 19:23
Subject:
Re: [perl #79908] my $x; sub(){$x} makes a constant evenif$xchanges
Message ID:
20101202213245.9680.qmail@lists-nntp.develooper.com
On 2010-12-02 10:33, Abigail wrote:
> On Wed, Dec 01, 2010 at 07:38:30PM +0100, Dr.Ruud wrote:
>> On 2010-12-01 00:24, Abigail wrote:
>>> On Tue, Nov 30, 2010 at 10:34:36PM +0100, Dr.Ruud wrote:
>>>> On 2010-11-29 10:22, Dave Mitchell wrote:
>>>>> On Sun, Nov 28, 2010 at 05:16:28PM -0800, Father Chrysostomos wrote:

>>>>>> I know this is what constant.pm used to use, but it causes a bug:
>>>>>
>>>>> What do you think is the bug? It seems to be behaving the way I expect.
>>>>>
>>>>>> $ perl -MO=Deparse -e' BEGIN{my $x = 5; *foo = sub(){$x}; $x=6} print foo'
>>>>>> sub BEGIN {
>>>>>>        my $x = 5;
>>>>>>        *foo = sub () {
>>>>>>            $x;
>>>>>>        }
>>>>>>        ;
>>>>>>        $x = 6;
>>>>>> }
>>>>>> print 5;
>>>>
>>>> The compiler should patch the constsub at the '$x = 6' line.
>>>
>>> If the compiler can detect that afterwards $x won't change - but you
>>> need to be able to solve the halting problem to do that in general.
>>> For instance,
>>>
>>>     BEGIN {
>>>        my $x = 5;
>>>        *foo = sub () {$x};
>>>        *bar = sub {$x = shift};
>>>     }
>>>
>>> the compiler could only patch the constsub if it knows bar() won't
>>> be called.
>>
>> And that is not '$x = CONSTANT', so that should then be a compile error.
>
> You're proposing a sub of the form 'sub {$x = shift;}' should be
> a compile error?

Indeed. The $x there, is (an alias for) a constant, and the compiler 
know that. The other option is to lose the constantification of foo(),
which is unwanted.

A 'const' keyword would help.

-- 
Ruud

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