in reply to scalar doesn't work values returned by a function?

But scalar is setting scalar context, I believe that is its sole purpose in life. The sort function can look to see if the context it is being used is scalar or list via the function wantarray. It seems that sort returns undef in a scalar context:
@a=('a', 'b', 'c'); $a = sort(@a); print $a; Use of uninitialized value at - line 3.
To put sort into a list context there is no clean way to do it, (sort(@a)) doesnt do it in this case. But a snip from the perlfunc man pages for scalar says:
There is no equivalent operator to force an expression to be interpolated in list context because it's in practice never needed. If you really wanted to do so, however, you could use the construction @{[ (some expression) ]}, but usually a simple (some expression) suffices.
So this does work with -w (but it is a bit of extra work):
@a=('a', 'b', 'c'); $a = @{[sort(@a)]}; print $a;

Replies are listed 'Best First'.
RE: Re: scalar doesn't work values returned by a function?
by Russ (Deacon) on May 10, 2000 at 07:11 UTC
    You're right, but note that @{[ sort @a ]} evaluates as an array, not a list ($a would contain '3', not 'c').

    This is probably not a problem, since ZZamboni's code doesn't seem to want the last value from the sorted list. It is, however an entirely plausible reason to try:   (To get the last sorted value) scalar(sort @array)

    This is just a bit of a bummer, since it forces some kinda ugly syntax to do something which ZZamboni's code feels like it should just naturally do.

    my @a = ('a','b','c'); print ((sort @a)[$#a]);
    Russ
      You are correct, $a would equal 3 in my last bit of code, because the sort will return a list and then store it in a referenced array which is then dereferenced and cast into scalar context which finally returns the size of the array. Got it? Good.
      I dont think I implied anything else though. I am not under the impression that ZZamboni wanted anything other than the size of the sorted array, but maybe I am wrong.

      If you wanted the last element of the sorted array of course you would do (sort @a)[-1] I certainly would not guess that scalar(sort @a) would return the last element, so I am glad that it doesnt, but apparently you think it should. I suppose you could write your own sort routine to do that in scalar context, but I like it the way it is. Just my opinion though.
        perlmonkey wrote: I certainly would not guess that scalar(sort @a) would return the last element, so I am glad that it doesnt, but apparently you think it should.

        From man perldata:

        In a context not requiring a list value, the value of the list literal is the value of the final element, as with the C comma operator. For example, @foo = ('cc', '-E', $bar); assigns the entire list value to array foo, but $foo = ('cc', '-E', $bar); assigns the value of variable bar to variable foo.
        Also:
        If you evaluate a named array in a scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value...
        From man perlfunc:
        sort LIST
        Sorts the LIST and returns the sorted list value.

        So, you see, since sort returns "the sorted list value," and lists evaluate to "the value of the final element," I would expect (at first glance) to get the last element from the list which sort returns.

        BTW, I hope I didn't offend you somehow. If so, I am terribly sorry. Your response seemed a bit hostile. Please accept my apologies...

        Just a nice guy trying to contribute,
        Russ