Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^5: Reference in Perl 6

by moritz (Cardinal)
on Aug 20, 2010 at 13:45 UTC ( [id://856276]=note: print w/replies, xml ) Need Help??


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

Sorry, I think I have over-reacted.

As I wrote before, everthing is a reference - except containers typed as native types, which exist for speed reasons (but aren't implemented in any compiler yet).

The typical use cases for explicit references in Perl 5 were nested data structures, creating aliases, and changing remote data.

For the former, you don't need any references in Perl 6 - you can simply insert a hash or array into an hash or array. In Perl 6, the question of whether a construct flattens into a list is orthogonal to references, and is mostly a syntactic distinction.

Aliasing is now done via binding, $a := $b aliases the variables $a and $b. Importing subroutines uses that mechanism under the hood.

Changing data that came in from somewhere else is done by an assignment, and the container is marked as is rw, for example

sub swap($a is rw, $b is rw) { ($a, $b) = ($b, $a) }

Object attributes also can have the is rw trait.

(People also sometimes say that a Capture is something like a fat reference, but I disagree; it is just a type of object that can hold other objects, and isn't different from user-defined types in that way, so I'm not going to elaborate on them).

Is there anything else you need to know for a coherent picture of references?

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

Replies are listed 'Best First'.
Re^6: Reference in Perl 6
by Anonymous Monk on Aug 20, 2010 at 14:23 UTC
    As I wrote before, everthing is a reference - except containers typed as native types,

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

    Aliasing is now done via binding,

    How would I create an alias (a reference) to a sub? Like this?:

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

    Thanks for the help. It looks like you have the beginnings of a perl6reftut here!

      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.

        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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://856276]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found