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

Aliasing operations in Perl 6

7 views
Skip to first unread message

Mark Jason Dominus

unread,
Apr 13, 2003, 3:02:01 PM4/13/03
to perl6-l...@perl.org

I'm trying to understand the effects of the binding operator,
particular as it relates to exportation.

1. I think that

$new := $old;
@new := @old;
%new := %old;

each perform aliasing operations, aliasing the variable named on the
left to refer to the value contained in the one on the right.

2. Similarly, if $aref contains an arrayref, then

@new := $aref;

aliases @new to the referred-to array, and if $href contains a
hashref, then

%new := $href;

aliases %new to the referred-to hash.

3. Now the puzzle. Suppose $sref contains a scalar reference, say one
which refers to the value "Carrots". Suppose I do

$new := $sref;

By (1), this aliases $new to contain the reference to 'Carrots'.
However, this behavior isn't analogous to the behavior in section (2),
since the analogous behavior would be for $new to be aliased to
'Carrots' itself.

Is it indeed (1) and not (2)? Or am I missing something important?

Thanks.

Uri Guttman

unread,
Apr 13, 2003, 3:19:49 PM4/13/03
to Mark Jason Dominus, perl6-l...@perl.org
>>>>> "MJD" == Mark Jason Dominus <m...@plover.com> writes:

MJD> 3. Now the puzzle. Suppose $sref contains a scalar reference, say one
MJD> which refers to the value "Carrots". Suppose I do

MJD> $new := $sref;

MJD> By (1), this aliases $new to contain the reference to 'Carrots'.
MJD> However, this behavior isn't analogous to the behavior in section (2),
MJD> since the analogous behavior would be for $new to be aliased to
MJD> 'Carrots' itself.

my take is that only when you are aliasing an aggregate ref to another
aggregate would that automatic deref occur. that is:

%foo := $href ;

doesn't deref $href immediately. but later on:

%foo{bar} # auto deref of the alias to the ref

would be the same as $href{bar}

in your scalar case there is no special way to do the deref so you have
to do it manually:

$foo = \"blah" ;
print $$foo ; # print 'blah'
$bar := $foo ;
print $bar # prints the ref and not 'blah'

if the scalar alias did an auto-deref as with the aggregates (and there
is no syntax for that AFAIK), then you couldn't get the ref value out of
the alias. the {} and [] parts are what trigger the deref and scalars
don't have anything like that.

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Mark Jason Dominus

unread,
Apr 13, 2003, 3:22:56 PM4/13/03
to Uri Guttman, Mark Jason Dominus, perl6-l...@perl.org

> >>>>> "MJD" == Mark Jason Dominus <m...@plover.com> writes:
>
> MJD> 3. Now the puzzle. Suppose $sref contains a scalar reference, say one
> MJD> which refers to the value "Carrots". Suppose I do
>
> MJD> $new := $sref;
>
> MJD> By (1), this aliases $new to contain the reference to 'Carrots'.
> MJD> However, this behavior isn't analogous to the behavior in section (2),
> MJD> since the analogous behavior would be for $new to be aliased to
> MJD> 'Carrots' itself.
>
> my take is that only when you are aliasing an aggregate ref to another
> aggregate would that automatic deref occur. that is:
>
> %foo := $href ;
>
> doesn't deref $href immediately.

Exegesis 3 seems to say otherwise:

my @costs := @%data{$file}{costs};

An important feature of the binding operator is that the lvalue (or
lvalues) on the left side form a context specification for the rvalue
(or rvalues) on the right side. It's as if the lvalues were the
parameters of an invisible subroutine, and the rvalues were the
corresponding arguments being passed to it. So, for example, we could
also have written:
my @costs := %data{$file}{costs};

(i.e. without the @ dereferencer) because the lvalue expects an array
as the corresponding rvalue, so Perl 6 automatically dereferences the
array reference in %data{$file}{costs} to provide that.


Uri Guttman

unread,
Apr 13, 2003, 3:43:37 PM4/13/03
to Mark Jason Dominus, perl6-l...@perl.org
>>>>> "MJD" == Mark Jason Dominus <m...@plover.com> writes:

>> my take is that only when you are aliasing an aggregate ref to another
>> aggregate would that automatic deref occur. that is:
>>
>> %foo := $href ;
>>
>> doesn't deref $href immediately.

MJD> Exegesis 3 seems to say otherwise:

MJD> my @costs := @%data{$file}{costs};

MJD> An important feature of the binding operator is that the
MJD> lvalue (or lvalues) on the left side form a context
MJD> specification for the rvalue (or rvalues) on the right side.
MJD> It's as if the lvalues were the parameters of an invisible
MJD> subroutine, and the rvalues were the corresponding arguments
MJD> being passed to it. So, for example, we could also have
MJD> written: my @costs := %data{$file}{costs};

MJD> (i.e. without the @ dereferencer) because the lvalue expects
MJD> an array as the corresponding rvalue, so Perl 6 automatically
MJD> dereferences the array reference in %data{$file}{costs} to
MJD> provide that.

that looks like %data{$file}{costs} is an array ref. i think the use of
'dereference' there is just the support of binding a ref to an aggregate
(vs binding to a scalar). the whole point of aliases is to not copy the
data so that use of dereferencing there is just syntax sugar since it
allows you to drop the logical (and not actual) deference of @.

i think that should be clarified and maybe the use of 'dereference' is
wrong there. maybe 'promotion' is a better term as the alias will
promote an aggregate ref to the appropriate aggregate if it is aliasing
to one. note that:

@foo := %bar ; # can't change aggregate types when aliasing

is illegal but:

@foo = %bar ; # should listify %bar. i don't think *%bar is needed

is fine.

$href := %hash ; # alias to the hash but as a ref
# the way $href = %hash assigns a ref

that should work too IMO and should be equivilent to:

$href := \%hash ;

that would be a 'demotion' vs. a 'promotion' :)

also, i think it should be called the alias operator as we had ~= as
bind in p5 (even with ~~ replacing ~= as smart match)

Damian Conway

unread,
Apr 13, 2003, 5:56:54 PM4/13/03
to perl6-l...@perl.org
Mark Jason Dominus wrote:

> 3. Now the puzzle. Suppose $sref contains a scalar reference, say one
> which refers to the value "Carrots". Suppose I do
>
> $new := $sref;
>
> By (1), this aliases $new to contain the reference to 'Carrots'.
> However, this behavior isn't analogous to the behavior in section (2),
> since the analogous behavior would be for $new to be aliased to
> 'Carrots' itself.
>
> Is it indeed (1) and not (2)? Or am I missing something important?


Scalars only autodereference in array or hash *contexts*.

So:

$ref = [1,2,3,4];
@var := $ref; # @var is another name for @$ref

$ref = {1=>2,3=>4};
%var := $ref; # %var is another name for %$ref

$ref = \$scalar;
$var := $ref; # $var is another name for $ref


Damian

Brent Dax

unread,
Apr 13, 2003, 10:33:16 PM4/13/03
to Mark Jason Dominus, perl6-l...@perl.org
Mark Jason Dominus:
# 'Carrots'. However, this behavior isn't analogous to the
# behavior in section (2), since the analogous behavior would
# be for $new to be aliased to 'Carrots' itself.

The analogous behavior for #3 isn't the ones in #2--it's the ones in #1.

$new := $old;
@new := @old;
%new := %old;

Are the analogous ones, because they all have matching sigils on both
sides.

@new := $old;
%new := $old;

Are analogous because they have mismatching sigils. Since there's no
way to mismatch a $ with a $, there isn't a $-on-the-left case there.

In other words: the analogy isn't in the sigil on the left--it's in how
well the sigil on the left matches the sigil on the right.

--Brent Dax <bren...@cpan.org>
@roles=map {"Parrot $_"} qw(embedding regexen Configure)

>How do you "test" this 'God' to "prove" it is who it says it is?
"If you're God, you know exactly what it would take to convince me. Do
that."
--Marc Fleury on alt.atheism


0 new messages