in reply to [Perl 6] $ and @ - what is it coming to?

Most people have been happy to drop the indirection operator -> between elements of a nested structure:

my @a = ( [ 1,2 ], [3,4] ); print $a[1]->[0]; ## => 3 print $a[1][0]; ## same thing

This just extends that convenience to the first level of dereference.

You have to jump through hoops, (assigning the ref to a glob ref), to achieve the same thing in Perl 5.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: [Perl 6] $ and @ - what is it coming to?
by ruzam (Curate) on Mar 26, 2008 at 18:32 UTC
    Funny, I started using '->' more as my Perl skills increased. Always seemed clearer to my eye.

    But more to the point, this confuses me:
    my @x = (1,2,3); my $x = ['x', 'y', 'z'];

    Pre-Perl 6 treats @x and $x as completely different variables. So I know that $x[1] is going to contain '2' and $x->[1] is going to contain 'y'. But according to the posts here, Perl 6 treats $x[1] as equivalent to $x->[1]. Just how is that supposed to work?

      As best I understand it, because in Perl 6, arrays always use the '@' sigil.

      So elements of @x are accessed using @x[1]

      And elements of the array pointed at by $x can be accessed using $x[1].

      The abiguity that is confusing you is because Perl 5 used $x[1] to access elements of array @x.

      @x = ( 1, 2, 3);; print @x[1];; Scalar value @x[1] better written as $x[1] at 2

      The explanation for that was that the use of the '$' signified that the element accessed was a scalar. But elements of arrays are always scalars, which makes the purpose of changing the sigil redundant. So in Perl 6 you don't.

      In the same way, if it hasn't changed since I last looked, hash elements are addressed using %x{'foo'} (or %x<foo>) and not $x{'foo'} as now.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        OK, thanks. I think I get it now.

        I've come to really love the way Perl identifies what you should expect to get out of the variable. It speaks to me on a "this is what I like about Perl" way. Now the emphasis seems to be on the container, not the contents which is a complete reversal. Some how that just seems like a really wrong thing to do with the language (is it even Perl anymore?). Maybe that's just a result of familiarity and I'd see it from the other side if Perl had always done it this way (shrug).
        Hey, wait a minute.

        If changing the sigil is considered redundant, then isn't leaving the '@' on it even more redundant? The array is already clearly identified by the [ ] no?

        (same comment to hashes)
      If you access an element of @x, you simple write @x[0] - not ambiguous in any way.

      It takes a while to get used to, but sigils are invariant in Perl 6.