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

Why doesn't shift / splice alias ??

Thread Next
From:
Elizabeth Mattijsen
Date:
September 13, 2010 08:45
Subject:
Why doesn't shift / splice alias ??
Message ID:
6259CAD6-86FC-4C91-A421-9AB1979DC649@dijkmat.nl
Inspired by a discussion on module-authors:

I've always wondered why:

  my $foo= shift @array;

would not be an aliasing operation.  Is there a specific reason why a copy is needed??

$ perl -le 'my @a=1; print \$a[0]; my $b= shift @a; print \$b'
SCALAR(0x506290)
SCALAR(0x523410)

Note that we now have 2 different addresses, which implies a copy.


$ perl -le 'my @a=1; print \$a[0]; print \$_ foreach shift @a;'
SCALAR(0x506290)
SCALAR(0x506290)

Note that foreach doesn't do this, even if we shift.


If shift() ( and splice, for that matter ) would become aliasing operations, then:

  my $self= shift;

would become a lot cheaper CPU wise, and that would be a boon to many object oriented modules.



Liz
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