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

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?

Replies are listed 'Best First'.
Re^3: [Perl 6] $ and @ - what is it coming to?
by BrowserUk (Patriarch) on Mar 26, 2008 at 18:59 UTC

    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).
        is it even Perl anymore?

        Yes. Write some, and I suspect you'll find that Perl 6 feels very Perlish.

        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...

        That's my guess.

      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)

        Well no. Then there would be no way to differentiate @x[1] and $x[1].

        Indeed, without the sigils it wouldn't be possible to have a scalar x and an array x and a hash x unless you also had to use the brackets and braces when referring to the container: x[] and x{}.

        But then how would you do an empty slice: @x[]?


        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.
Re^3: [Perl 6] $ and @ - what is it coming to?
by moritz (Cardinal) on Mar 31, 2008 at 09:04 UTC
    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.