develooper Front page | perl.perl5.porters | Postings from April 2018

Re: [perl #133109] push on scalar forbidden: why?

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
April 21, 2018 15:37
Subject:
Re: [perl #133109] push on scalar forbidden: why?
Message ID:
20180421153717.GG2839@iabyn.com
On Thu, Apr 19, 2018 at 06:37:26PM -0700, L A Walsh wrote:
> Dan Book via RT wrote:
> > On Thu, Apr 19, 2018 at 6:09 PM, L A Walsh <perl-diddler@tlinx.org> wrote:
> > ...
> > 
> > >    The manpage shows:
> > >       Functions for real @ARRAYs
> > >           "each", "keys", "pop", "push", "shift", "splice", "unshift",
> > >           "values"
> > > 
> > >       Functions for real %HASHes
> > >           "delete", "each", "exists", "keys", "values"
> > > 
> > 
> > each, keys, and values work on both arrays and hashes. As does exists and
> > delete, technically.
> > 
> > -Dan
> > 
> Yes...for dereferencing to work in this case, the scalar would have to
> hold either an ARRAY or HASH reference.  It really makes no sense to try
> to use "each", "keys" nor "values" on a a scalar that has value 'undef'.
> It would make no sense and would (should) generate an error as it does
> now.

But when you have a scalar reference which is overloaded, perl can't know
what overload method to call in those cases. For example:

    use overload
        '@{}' => sub { print "array deref\n" ; return \@real_a },
        '%{}' => sub { print "hash  deref\n" ; return \%real_h },
        ;


    my $r = bless [];

    push $r, 3;        # prints "array derdef", modifies @real_a
    $r->{foo} = 'bar'; # prints "hash  derdef", modifies %real_h
    @k = keys $r;      # what overload method does this call?

What should perl do in the last case?

-- 
The optimist believes that he lives in the best of all possible worlds.
As does the pessimist.

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