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

It's a bit confusing at first that Perl 6 has different scalar contexts, called "item context".

In generic item context an array returns a capture (think reference) to an array.

But in other item contexts, like boolean ?@array, numeric +@array and string ~@array the array will return different things (for example number of elements, and a string representation).

A scalar value that holds an array capture automatically dereferences, so you can write $ary[0] instead of the cludgy $ary->[0].

I don't know why this should make sigils useless. Sigils serve two purposes: visual distinction, and name disambiguation (%foo is a different variable than @foo).

Automatic dereferencing doesn't impact any of those.

It's like in Perl 5 - you can store everything in a scalar (or scalar ref), but you shouldn't, because it makes your program less readable.

But if you decide to do that in Perl 6, the syntax is nicer ;-)

Update: as a side note the braces after a variable are nothing special or magical in Perl 6, it just calls  method postcircumfix:<[ ]>($invocant:, $argument) on the variable. So it's just natural that it works on a capture as well as on an array.

Replies are listed 'Best First'.
Re^2: [Perl 6] $ and @ - what is it coming to?
by amarquis (Curate) on Mar 25, 2008 at 15:47 UTC

    Is the reason that scalar context has been renamed item context to give it some symmetry with arrays/lists? So now we can have arrays associated with groupings (list context) and scalars associated with single objects (item context)? I was futzing around the synopses trying to answer this, and didn't come to a conclusion.

    Looking through all of this, I'm sort of shocked at how much Perl 6 I've missed out on. These are very interesting reading, and I wasn't aware that design was this far along. (I've not followed Perl 6 at all)

Re^2: [Perl 6] $ and @ - what is it coming to?
by John M. Dlugosz (Monsignor) on Mar 27, 2008 at 04:26 UTC
    Sigils serve two purposes: visual distinction, and name disambiguation (%foo is a different variable than @foo).

    Automatic dereferencing doesn't impact any of those.

    I'm not convinced. What if an Object is a collection class of some kind? And it only is useful on the top-level, as anything already inside another collection is a scalar.

    —John
    Writing from Pudong Airport

      What if an Object is a collection class of some kind?
      Could you elaborate on why this is could be a problem?
        The object might be "plural" to your mind, but is accessed via the object syntax $x.foo rather than being a @-sigil list variable.

        Upon further reading, I understand that @ variables can be bound at compile-time to any class that supports the Positional role. Though I think that can lead to subtle issues as well, such as losing the class when doing assignment.

        —John