develooper Front page | perl.perl5.porters | Postings from May 2016

[perl #128187] Make list assignment to sub :lvalue{keys} an error

Thread Previous
From:
Father Chrysostomos
Date:
May 19, 2016 18:30
Subject:
[perl #128187] Make list assignment to sub :lvalue{keys} an error
Message ID:
rt-4.0.18-10271-1463682635-762.128187-75-0@perl.org
# New Ticket Created by  Father Chrysostomos 
# Please include the string:  [perl #128187]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=128187 >


With the %hash{'key'} syntax, which is not an lvalue, we have an error if it is the last statement in an lvalue sub and the lvalue sub is assigned to.  However, if the lvalue sub call is in foreach(...) or referenced by \ we have no error.

So, given

    sub foo :lvalue { %hash{'key'} }

the first line is permitted, but the second one dies:

    \foo;
    foo = 3;

even though these are both variants of lvalue context.

I propose extending that to keys() returned from an lvalue sub in a list assignement.  Currently, this works:

    sub foo :lvalue { keys %h }
    foo = 1024;

But this does not die, yet does nothing useful, and is probably a mistake wherever it occurs:

    sub foo :lvalue { keys %h }
    (foo) = 1024;  # I want this to die.

whereas the plain form of list assignment to keys *does* die at compile time:

    (keys %h) = 1024; # Can't modify keys in list assignment

The reason I bring this up is that, with the demise of autoderef, the one thing standing in the way of making &CORE::keys() work is gone, and I am trying to implement it now.  It will be much easier to make this behave the same way as a bare keys()--

    &CORE::keys(\%h) = 1024; # ok; scalar assignment
    (&CORE::keys(\%h)) = 1024; # not ok; list assignment

--if I can disallow keys in the circumstance described above.  After all, &CORE::keys will be an lvalue sub with ‘keys’ (or the next best thing) as the last op.

I intend to go ahead and make this change soon (whatever soon means), but you still have until next year’s code freeze to object. :-)

-- 

Father Chrysostomos


Thread Previous


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