Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Aliasing swapped values

4 views
Skip to first unread message

Ovid

unread,
Apr 10, 2005, 1:47:51 PM4/10/05
to perl6-l...@perl.org
Hi all,

Apologies if this has been covered. What should this do?

($x,$y) := ($y,$x);

In Perl5:

$x=2; $y=3;
print "x: $x y: $y\n";
(*::x, *::y) = (*::y, *::x);
$y=4;
print "x: $x y: $y\n";
$x=5;
print "x: $x y: $y\n";

This program shows typeglob aliasing. If we try to alias swapped
value, we merely swap what they are pointing at and we haven't aliased
anything. I am not sure this should be appropriate behavior for Perl6.
Aliasing will be much more common in Perl6 so this will be a trap for
many. In fact, consider this:

($c, $d) := ($y, $x);
# later
($x,$y) := ($c, $d); # whoops!

That's going to confuse the heck out of people. If we do as Perl5
does, we'll merely be swapping values since aliasing really doesn't
make much sense here, but that means for this case, ":=" == "=".

Cheers,
Ovid

--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/

Juerd

unread,
Apr 10, 2005, 5:59:57 PM4/10/05
to Ovid, perl6-l...@perl.org
Ovid skribis 2005-04-10 10:47 (-0700):

> Apologies if this has been covered. What should this do?
> ($x,$y) := ($y,$x);

It would let $x be a second name for the variable that is also called
$y, and $y for $x. The old names $x and $y are overwritten, so
essentially the names for the two variables swapped without copying or
moving values around.

This is assuming that bound variables are visible only after the entire
bind expression is handled. Which would have to mean that the
expressions for default values are special.

> ($c, $d) := ($y, $x);
> # later
> ($x,$y) := ($c, $d); # whoops!

I imagine that that has the same effect as

($x, $y) := ($y, $x);
($c, $d) := ($x, $y);

Of which I wonder if it can be written as

($c, $d) := ($x, $y) := ($y, $x);


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

0 new messages