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

I've been going over the S documents, and I'm still not seeing what is the difference between accessing a single value and accessing multiple values from an array?

$x= @a[4]; @y= @a[4,5,6]; $q= @a[@s];
I suppose that the compiler figures out the first two based on static analysis. But what about the third? If @s contains exactly one value, will $q wind up with an item or a reference to a list of one item?

Normally the context (item, list, etc) is determined by context, hence its name. But is the type of expression instead driving what the subscripting operator does, in the manner of multimethod overloading?

—John

Replies are listed 'Best First'.
Re: [Perl 6] single-value vs. array slices
by moritz (Cardinal) on Mar 31, 2008 at 16:03 UTC
    An array subscript will, by default, be evaluated in slice context.

    Note that Perl 6 signature allow multi method dispatch that distinguishes between these two cases;

    my @a = 1; my_sub(@a, 2); # and my_sub(1, 2);

    This is possible because lists don't flatten in slice context. (Slice context is something like multi-dimensional list context).

    So I know that @a[1] returns a single scalar value, the one that is stored in @a. @a[@other_list_with_many_values] will always produce a list, which is transformed to a capture (think reference here) in generic item context, and to the number of elements in numeric context.

    I don't know what @a[@list_with_one_element] returns, but it seems logical that it returns a list as well. If you want it to return a single element, not a list, you can flatten the array:

    @a[|@list_with_one_element];
      TimToady stopped by the CB while this thread was being discussed, and said that by default the brackets always work in list context. He and some others pointed to S02:1377 as the documentation. I've not been keeping very current lately on Perl6, but I hope the link helps.
        Yes, that very section in the documentation was updated already this month based on problems I found. The example was showing context changes in "other places", that is clearly against the rules now.

        —John

Re: [Perl 6] single-value vs. array slices
by Anonymous Monk on Mar 30, 2008 at 13:08 UTC
    http://dev.perl.org/perl6/doc/design/exe/E02.html
    ... For the time being, it's enough to know that, if you put a single scalar in the square brackets, you get a single element look-up; if you put a list in the brackets, you get a slice.
    [Update: Lvalue slices now require parens around then whole left side.]
      Insufficient.

      What if the thing in the brackets is a function call? Normally the context drives whether the function "wants" to return a list or scalar. Instead, it seems that what the function does return matters? But then context is propagating in the wrong direction at runtime.

      Also, is this a case where it really does treat a @name variable different from a scalar variable that holds a (ref to) a list? That could be an answer to a previous question, for real differences.

      Can you give an example of your update, too? My reading teaches me that parens are only used for grouping, not to trigger context. (OTOH returning pairs seems to violate this principle too)

      —John

        I don't know :) but you can force context for function calls to be anything you want