Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > @_ aliasing

Reply
Thread Tools

@_ aliasing

 
 
Matija Papec
Guest
Posts: n/a
 
      09-22-2005

Is there a perl version which prints 5 in case when,
perl -le 'sub f {my $a = shift; $a=5} f(my $b=1); print $b'

I was almost sure that shift() should not break the aliasing.

 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      09-22-2005
Matija Papec wrote:

> Is there a perl version which prints 5 in case when,
> perl -le 'sub f {my $a = shift; $a=5} f(my $b=1); print $b'
> I was almost sure that shift() should not break the aliasing.


perl -le "sub f { $_=5 for shift } f(my $b=1); print $b"

should do.

--
Bart

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-22-2005
Matija Papec wrote:
> Is there a perl version which prints 5 in case when,
> perl -le 'sub f {my $a = shift; $a=5} f(my $b=1); print $b'


I can't imagine why such a thing would happen.

> I was almost sure that shift() should not break the aliasing.


shift() isn't breaking any aliasing. @_ is aliased to the arguments of
f().
`my $a = shift;` says to remove the first element of @_ and return a
copy of it. This is no different than if you had said
my $a = $_[0];
$_[0] is an alias to the first argument of f(). $a is not. The only
difference between this and your statement is that your statement also
has the effect of removing the first element of @_.

If you want to change the argument to f(), change it directly:
$_[0] = 5;

Paul Lalli

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      09-22-2005
Matija Papec <(E-Mail Removed)> wrote:
> Is there a perl version which prints 5 in case when,
> perl -le 'sub f {my $a = shift; $a=5} f(my $b=1); print $b'
>
> I was almost sure that shift() should not break the aliasing.


It is the assignment, not the shift, which is breaking the aliasing.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      09-22-2005
X-Ftn-To: Bart Van der Donck

"Bart Van der Donck" <(E-Mail Removed)> wrote:
>> perl -le 'sub f {my $a = shift; $a=5} f(my $b=1); print $b'
>> I was almost sure that shift() should not break the aliasing.

>
>perl -le "sub f { $_=5 for shift } f(my $b=1); print $b"
>
>should do.


interesting, tnx!


--
Matija
 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      09-22-2005
X-Ftn-To: Paul Lalli

"Paul Lalli" <(E-Mail Removed)> wrote:
>> I was almost sure that shift() should not break the aliasing.

>
>shift() isn't breaking any aliasing. @_ is aliased to the arguments of
>f().
>`my $a = shift;` says to remove the first element of @_ and return a
>copy of it. This is no different than if you had said
>my $a = $_[0];
>$_[0] is an alias to the first argument of f(). $a is not. The only
>difference between this and your statement is that your statement also
>has the effect of removing the first element of @_.
>
>If you want to change the argument to f(), change it directly:
>$_[0] = 5;


Yes, no doubt about that, but I think that I've read somewhere how shift
keeps the "magic" when operating on @_.

btw, Bart showed something interesting,
perl -le "sub f { $_=5 for shift } f(my $b=1); print $b"

should be doing the same thing as,
perl -le "sub f { $_=5 for map $_, shift } f(my $b=1); print $b"

but it doesn't?


--
Matija
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-22-2005
Matija Papec wrote:
> X-Ftn-To: Paul Lalli
>
> "Paul Lalli" <(E-Mail Removed)> wrote:
> >If you want to change the argument to f(), change it directly:
> >$_[0] = 5;

>
> Yes, no doubt about that, but I think that I've read somewhere how shift
> keeps the "magic" when operating on @_.


Well, since your expected results clearly do not mesh with reality, it
would seem that either 1) What you read was incorrect, or 2) You are
misremembering or misunderstanding what you read. If you manage to
find the citation you're talking about, please feel free to post it
here for our examination...

> btw, Bart showed something interesting,
> perl -le "sub f { $_=5 for shift } f(my $b=1); print $b"
>
> should be doing the same thing as,
> perl -le "sub f { $_=5 for map $_, shift } f(my $b=1); print $b"
>
> but it doesn't?


I don't understand why you're assuming they *should* do the same thing.
Let's stop using shortcuts and spell each of these out:
sub f {
foreach (shift @_){
$_ = 5;
}
}
In this one, $_ is an alias to each element of the list that foreach
iterates over. @_ contains a list of aliases to f()'s arguments.
shift() remove and returns the first element of @_. So the list that
foreach iterates over is the first element of @_. Therefore, $_ is an
alias to an alias to the first argument of f().

sub f {
foreach (map { $_ } shift @_) {
$_ = 5;
}
}

In this one, foreach is not iterating over the first element of @_.
Instead, you're using map() to generate a new list, which will be
composed of the values passed to it. Within the map block itself, $_
is an alias to the first element of @_. The list that map returns
however, is not an alias. It is this list that foreach is iterating
over.

Paul Lalli

 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      09-23-2005
On 22 Sep 2005 13:26:23 -0700, "Paul Lalli" <(E-Mail Removed)> wrote:

>I don't understand why you're assuming they *should* do the same thing.
> Let's stop using shortcuts and spell each of these out:
>sub f {
> foreach (shift @_){
> $_ = 5;
> }
>}
>In this one, $_ is an alias to each element of the list that foreach
>iterates over.


ok.

>@_ contains a list of aliases to f()'s arguments.
>shift() remove and returns the first element of @_.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ok.

>So the list that
>foreach iterates over is the first element of @_.


How so? Doesn't foreach iterate over already shifted value which was
previously _removed_ from @_?
IMO, if it's removed from @_, it doesn't belong to @_ anymore..?

>sub f {
> foreach (map { $_ } shift @_) {
> $_ = 5;
> }
>}
>
>In this one, foreach is not iterating over the first element of @_.


Yes, it does not iterate over @_; I wrote it as the more obvious
example of non @_ aliasing.

 
Reply With Quote
 
Matija Papec
Guest
Posts: n/a
 
      09-23-2005
On Thu, 22 Sep 2005 19:24:08 GMT, Tim Hammerquist <(E-Mail Removed)>
wrote:

>> > Yes, no doubt about that, but I think that I've read
>> > somewhere how shift keeps the "magic" when operating on @_.

>>
>> You probably read that, in order to avoid confusing,
>> hard-to-find errors involving inadvertently changing aliased
>> arguments, you should probably be making explicit copies of them
>> by assigning them to lexically scoped vars unless you're sure
>> you want to change the aliased variables.

>
>Or possibly the magic of shift() you mean is that, at package
>scope, it operates on @ARGV by default, but inside a sub, it
>defaults to @_.


It could be, but that was a few years ago and my memory isn't doing me
the justice.

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-23-2005
Paul Lalli wrote:

> Matija Papec wrote:
>
>>Is there a perl version which prints 5 in case when,
>>perl -le 'sub f {my $a = shift; $a=5} f(my $b=1); print $b'
>>I was almost sure that shift() should not break the aliasing.

>
> shift() isn't breaking any aliasing.


> If you want to change the argument to f(), change it directly:
> $_[0] = 5;


Or

my $a = \shift; # or \$_[0]
$$a = 5;

Or see the Lexical::Alias module.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
aliasing palmis Java 0 02-02-2006 08:32 PM
More anti-aliasing puzzles Roedy Green Java 25 08-16-2005 07:41 PM
Aliasing a class name? Wesley T Perkins Java 8 07-01-2005 01:56 AM
Anti-aliasing GIF Images Kevin Bertman Java 4 11-29-2004 05:46 AM
LCD anti-aliasing in Java Tim Tyler Java 2 09-05-2003 09:01 AM



Advertisments