Front page | perl.perl5.porters |
Postings from April 2018
Re: [perl #133109] push on scalar forbidden: why?
Thread Previous
|
Thread Next
From:
Philip R Brenan
Date:
April 21, 2018 17:17
Subject:
Re: [perl #133109] push on scalar forbidden: why?
Message ID:
CALhwFRm0MuJSWYnB41yZocvbbUnGJTfF=LNC0RK6cAibVBXc-Q@mail.gmail.com
Presumably it would return () as $r is still a reference to an empty array
despite the ingenious use of side effects implemented via operator
overloading to manipulate other arrays and hashes and thereby give the
impression that $r is both an array and a hash?
If push $r, 3 were always interpreted identically to push @$r, 3 we would
expect to get:
use Data::Dump qw(dump);
my @real_a;
my %real_h;
use overload
'@{}' => sub { print "array deref\n" ; return \@real_a },
'%{}' => sub { print "hash deref\n" ; return \%real_h },
;
my $r = bless [];
push *@$r*, 3;
say STDERR "AAAA ", dump($r); # AAAA bless([3], "main")
say STDERR "BBBB ", dump(\@real_a); # BBBB [3]
I.e. $r is not empty, which illustrates that push $r and push @$r would not
always produce identical results under the proposed scheme in the presence
of operator loading.
I urge that both of these problems can be overcome by Linda's statement of
the the scheme:
*Push gets its array either directly or via a reference. *In this example:
push $r, 3
would affect only $r, not @real_a, because no array dereference would be
implied or performed.
On Sat, Apr 21, 2018 at 4:37 PM, Dave Mitchell <davem@iabyn.com> wrote:
> 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.
>
--
Thanks,
Phil <http://www.appaapps.com/howToWriteAnApp.html>
Philip R Brenan <http://www.appaapps.com/howToWriteAnApp.html>
Thread Previous
|
Thread Next