John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

pugs> my @y= 1,2,3 (1, 2, 3) pugs> @y.WHAT ::Array pugs> my $x= (1,2,3) (1, 2, 3) pugs> $x (1, 2, 3) pugs> $x.WHAT ::Array pugs> my $z= [1,2,3] (1, 2, 3) pugs> $z.WHAT ::Array pugs> $x[1] 2 pugs> @y[1] 2 pugs> $z[1] 2 pugs>
OK, why are we making a fundamental distinction between scalar and list variables via the sigil, when they seem to behave identically? A scalar can hold a list, and the explicit "reference" concept is no longer used, and the only difference is that you can leave off the parens when assigning a literal to the list variable.

Given that scalers can hold arrays, why bother with making @variables different, or using them in code?

—John
Writing from a comfortable room in 上黑 (Shanghei)上海 (Shanghai)

Replies are listed 'Best First'.
Re: [Perl 6] $ and @ - what is it coming to?
by moritz (Cardinal) on Mar 25, 2008 at 15:01 UTC
    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.

      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)

      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?
Re: [Perl 6] $ and @ - what is it coming to?
by BrowserUk (Patriarch) on Mar 25, 2008 at 15:57 UTC

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

Re: [Perl 6] $ and @ - what is it coming to?
by Starky (Chaplain) on Mar 28, 2008 at 11:45 UTC
    'Scuse me for being off-topic, but I've been working on my Chinese geography. I know 上海 (Shanghai), the largest (population-wise) city in China and the industrial heart of the mainland. Where is 上黑 (Shanghei)? Googling just resulted in a bunch of misspellings of Shanghai.

    --
    Writing from a comfortable room in 昆明 (Kunming)
      No! http://en.wikipedia.org/wiki/Shanghai
      Just a typo, sorry! That's what I get for trying to type Chinese without my wife's help.

      Sorry I missed you in 昆明. I was there for an extended stay.

      —John
      back home in Texas