in reply to Re^7: Reference in Perl 6
in thread Reference in Perl 6

Ok, so,

sub foo($x, $y) { say $x ~ $y }; my $foo_ref = &foo;

assigns a reference to $foo_ref (makes sense (since I don't see why I'd want to copy a function), and

my @bar = <a b c>; my $bar_ref = @bar; # Assigns a reference. Ok. my $bar_ref2 := @bar # Same as above? XXX my @bar_copy = @bar; # Makes a copy of @bar. As expected.

and

my $qux = SomeClass.new; # $qux is a reference to an object my $qux_ref = $qux; # makes a copy of the reference (?)

Does that copy the object referred to by $qux, or just the reference?

However,

my $xyz = 'hi'; my $xyz_ref = $xyz; # not a ref!

copies $xyz as expected. Correct?

Replies are listed 'Best First'.
Re^9: Reference in Perl 6
by moritz (Cardinal) on Aug 23, 2010 at 12:10 UTC

    Stop asking "is $var a reference to $object" - since nearly everything is a reference, it doesn't make too much sense.

    Instead, you could ask questions like "if I change $var1, does $var2 change too?"

    Those questions are easy to answer - you can just try them out.

    You can also think of a variable being a name for a container, and that container holds a value. Assignment changes the value, binding the container.

    Does that copy the object referred to by $qux

    No. Assignment to a scalar never copies any object (just pointers, internally). Only if you assign to an array or hash variable (ie one beginning with @ or %), a shallow copy is made.

    my $xyz = 'hi'; my $xyz_ref = $xyz; # not a ref!

    copies $xyz as expected. Correct?

    No, just copies a reference to the string 'hi'. But since strings are immutable, you won't see any diference between two references to the same string, and two reference to copies of the same string.

    Perl 6 - links to (nearly) everything that is Perl 6.

      Thanks for the reply, moritz.