Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl 6, differrence between ':=' and '='

by Anonymous Monk
on Aug 12, 2010 at 04:38 UTC ( [id://854572]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In Perl 6 what is the difference between ':=' and '=' , can some body please explain in detail with an example?
  • Comment on Perl 6, differrence between ':=' and '='

Replies are listed 'Best First'.
Re: Perl 6, difference between ':=' and '='
by chromatic (Archbishop) on Aug 12, 2010 at 06:04 UTC

    From Synopsis 03:

    my $x = 'Just Another'; my $y := $x; $y = 'Perl Hacker'; say $x; say $y;

    The assignment operator = assigns the value of its right operand to the container which is its left operand. The binding operator := associates the container of its right operand to the name which is its left operand.

    Think of it a bit like Perl 5 references without the reference or dereference operators.

      I always understood it as closer to typeglob aliasing in Perl 5 rather than perl 5 style references. i.e.:
      # perl 6 $x := $y; # ~= perl 5 *x = \$y;

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

        That's the best way to think of it, but I didn't want to explain typeglobs.

Re: Perl 6, differrence between ':=' and '='
by moritz (Cardinal) on Aug 12, 2010 at 06:31 UTC
    As chromatic already explained, := creates an alias, while = puts a value into a container.

    What's more is that = also enforces a context, so for example assigning to an array variable causes it to upgrade the right-hand side to an array. Binding doesn't:

    $ perl6 -e 'my @a = 1, 2, 3; push @a, 4; say ~@a' 1 2 3 4 $ perl6 -e 'my @a := 1, 2, 3; push @a, 4; say ~@a' Method '!fill' not found for invocant of class 'Int' in 'List::push' at line 2617:CORE.setting in main program body at line 1

    The error message isn't good, but what happened is that I tried to push onto a list of values (not to an Array, because the binding replaced the array variable with a List). A List is read-only, so it blew up.

    Assigning to arrays is also eager, whereas binding doesn't evaluate lists at all:

    my @a = gather for 1..3 { .say; take $_; } say "printing now..."; say ~@a; # produces 1 2 3 printing now... 1 2 3

    But with binding, you get lazy evaluation of the gather/take construct:

    my @a := gather for 1..3 { .say; take $_; } say "printing now..."; say ~@a; # produces printing now... 1 2 3 1 2 3

    Only on accessing list items are they evaluated as needed.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://854572]
Approved by prasadbabu
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2025-12-13 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (93 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.