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

So, everything except arrays and hashes? Is "$foo" in "my $foo = 'hi'" a reference?

$foo is variable. If you do a

my $foo = Something.new();

then the newly created object is stored by reference in the variable. It's the same for string literals, but since strings and immutable (and so-called "value types"), it doesn't make a difference for strings.

How would I create an alias (a reference) to a sub?
sub foo($x, $y) { say $x ~ $y } my $foo_ref = &foo; # leaves $foo_ref writable my $foo := &foo; # basically the same, but you can't # assign to $foo afterwards # passing values to a signature is a form of # binding too, so this works: sub c(&func) { func(42, 23); } c(&foo); c($foo); c($foo_ref);
Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^8: Reference in Perl 6
by Anonymous Monk on Aug 20, 2010 at 16:42 UTC

    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?

      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.