in reply to Scalar context of slice

A slice returns a list and assigning a list to a scalar will just assign the last item in the list to the scalar. It's just the same as

Update: A slice in scalar context returns the last item in the slice, and that will be assigned to $broken_count. It's similar in effect to

my $foo = (1,2,3); # assigns 3 to $foo

map in scalar context returns what you're after: the length of the list. See map.

You may be more enlightened by the following small modification of your program:

my %bar = ( a => 'A', b => 'B', c => 'C', d => 'D', ); my $broken_count = @bar{'a', 'b'}; my $working_count = map {$_} @bar{'a', 'b'}; print "broken_count : $broken_count\n"; # B print "working_count: $working_count\n"; # 2

Update: Listy things in scalary context (there's no such thing as a "list in scalar context") are all special cased. You need to consult the relevant docs to see what will happen for each specific case.


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^2: Scalar context of slice (myth)
by tye (Sage) on Oct 03, 2008 at 21:06 UTC
    Why doesn't scalar context of a slice (as follows) give the length of the list?

    Quite simply because when I noticed that using a slice in scalar context caused Perl to core dump, I patched it to return the last item instead. Actually, that was probably only for hash slices (it was a long time ago).

    More sensibly, it is good that the simple typo/thinko of @array[$index] still produces the same value as $array[$index], not just being a silly way to write 1. What justification do you have to proclaim that the size of the slice is the best choice?

    A slice returns a list and assigning a list to a scalar will just assign the last item in the list to the scalar.

    map and grep return lists so, obviously, in a scalar context they also return the last item (according to you). Except they don't, of course.

    I'm repeatedly surprised at who next spouts this "a list in scalar context" meme that refuses to die. It is a seductive meme, but it is at least misleading (or just "wrong", if you ask me).

    Expressions that return a list of values in a list context can choose to return whatever makes the most sense when they find themselves in a scalar context. For grep the number of items makes good sense. For map, I don't think that returning the count is the best choice, but that ship sailed long, long ago. For a hash slice, dieing might have been a better choice, but that didn't even occur to me at the time I wrote that patch and it appears nobody since has felt the need to change that behavior.

    - tye        

      map and grep return lists so, obviously, in a scalar context they also return the last item (according to you). Except they don't, of course.
      Of course they don't. That's why I wrote "map in scalar context returns what you're after: the length of the list. See map."

      I really don't see what point you're trying to make tye :-(

        You said:

        A slice returns a list and assigning a list to a scalar will just assign the last item in the list to the scalar.

        And map and grep each return lists. So your above proclamation would imply that map and grep "will just assign the last item in the list to the scalar". So do you think that map doesn't return a list? Or do you retract your first sentence?

        - tye