On 8/14/07, Jay Savage <[EMAIL PROTECTED]> wrote:
snip
> No. a single scalar subscript is, well, a scalar, not a single-element
> list. And it refers to an index, not a slice. The definition of a
> slice is more than one. from perldata:
>
> "A slice accesses several elements of a list, an array, or a hash
> simultaneously using a list of subscripts. It's more convenient than
> writing out the individual elements as a list of separate scalar
> values."
>
> Therefore:
>
>     (qw/one two three four)[0]; # not a slice
>     (qw/one two three four)[2,3]; # slice
snip

Argue with the error messages then:

perl -e '($a)[0] = 5'
Can't modify list slice in scalar assignment at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

Also, you can prove that @array[0] is a list (not a scalar) by using
the poorly named wantarray* function:

perl -le 'sub a { wantarray ? "t" : "f" } @a[0] = a(); print @a'
t

You seem to be caught up on the word "several" in the quote from the
docs.  That was an unfortunate choice of words on their part.  You
also might be thinking that "a list of subscripts" must contain a
comma or a range, but there is such a thing as a list of one element,
and subscripts on lists and arrays slices are such a list.  This can
be proven with wantarray again:

perl -le 'sub a { wantarray } $_ = ("f", "t")[a()]; print'
t
perl -le 'sub a { wantarray } @a = qw<f t>; print @a[a()]'
t

Oddly enough, array indexing, rather than slicing, puts the contents
of [] in a scalar context:

perl -le 'sub a { wantarray } @a = qw<f t>; print $a[a()]'
f

snip
> For that to be true, the subscript notation itself would have to force
> list context. It demonstrably doesn't. Consider:
>
>      @bar = (1,2,3);
>      @baz = (1,2,3,4,5);
>      $foo = @[EMAIL PROTECTED];
>      ($foo) = @[EMAIL PROTECTED];
>
> As you can see, the slice or index takes its context from the
> surrounding code, just as we would expect. The presence of square
> brackets doesn't somehow turn things that would otherwise be scalars
> into lists.
snip

The notation @baz[...] can only mean two things to a reasonable
person: a list or an array.  In Perl the choice was made that it would
be a list (array would have been perfectly valid choice, but in
general Perl prefers lists to arrays).  It still a list even if ... is
an empty list or a scalar value (but in a list context, see above).
In the first assignment to $foo a list is being put in scalar context,
so 4 is assigned to $foo (last item of the list) and in the second
example a list is being assigned to a list, so $foo is 1 (the first
item of the list).  In some mythical language Prel (and a very clean
language it is) where the designer chose to make @baz[...] an array
the following two statements both result in $foo being assigned the
number of elements** in @bar.

$foo = @[EMAIL PROTECTED];
$foo = @bar;


Now, Perl 6 seems to following the similar rules:

from S02***
    (*) Slicing is specified by the nature of the subscript, not by the sigil.
    (*) The context in which a subscript is evaluated is no longer controlled
        by the sigil either. Subscripts are always evaluated in list context.

        If you need to force inner context to scalar, we now have convenient
        single-character context specifiers such as + for numbers and
~ for strings:

        @x[f()]   =  g();       # list context for f() and g()
        @x[f()]   = +g();       # list context for f(), scalar context for g()
        @x[+f()]  =  g();       # scalar context for f() and g()
                                # -- see S03 for "SIMPLE" lvalues

        @x[f()]   =  @y[g()];   # list context for f() and g()
        @x[f()]   = [EMAIL PROTECTED]()];   # list context for f() and g()
        @x[+f()]  =  @y[g()];   # scalar context for f(), list context for g()
        @x[f()]   =  @y[+g()];  # list context for f(), scalar context for g()



* it should have been named wantlist or maybe just wants (so it could
handle all contexts, not just scalar vs list), see perldoc -f
wantarray
** assuming the designer allowed accessing elements of an array that
are outside its bounds, it would be an runtime error or the smaller of
the two array sizes otherwise.
*** http://dev.perl.org/perl6/doc/design/syn/S02.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to