in reply to RE: RE: Re: scalar doesn't work values returned by a function?
in thread scalar doesn't work values returned by a function?

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

  • Comment on RE: RE: RE: Re: scalar doesn't work values returned by a function?

Replies are listed 'Best First'.
RE: RE: RE: RE: Re: scalar doesn't work values returned by a function?
by perlmonkey (Hermit) on May 11, 2000 at 09:12 UTC
    Thanks for the spelling out your logic, I see now why you thought it might have been the last value. Interesting stuff. I am, apparently, not that hip on the list vs array scene.

    No I wasn't trying to be hostile, too bad tone is not available in text (without the dumb little smileys anyway). I was actually trying for a subtle humor thing, but I guess it was way to subtle.
    I appreciated your input.

    Thanks
    -Cory
Re: scalar doesn't work values returned by a function?
by ZZamboni (Curate) on May 11, 2000 at 09:37 UTC
    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...

    Ahhh, this clarifies a lot. Most of the answers are in the documentation, it's just so hard to keep track of all of it...

    But it still does not explain why scalar(sort @a) returns undef. If sort @a returns a list, and a list in scalar context gets you the last value... or does it only apply to named lists?

    Thank you all for your responses. This thread has been very enlightening for me.

    --ZZamboni

      I think you might be missing one of the finer points of perl.

      Functions can return completly different value depending on what context they are called in. sort does not always return a list, it usually does. If sort is called is a scalar context then the function returns undef. It does not return a list if called in scalar context. So the scalar function is not placing the return value of sort in scalar context it is placing the the actual function call in scalar context.

      Here is some code for demonstration:
      This is what Russ was refering to
      $scalar = (5,10,15,20); print $scalar, "\n";
      Results:
      20


      This creates a function which will return different values depending on the context that the function was called in:
      sub get_list { # return a list if a list is wanted, else # return the string return wantarray ? (5,10,15,20) : "SCALAR CALL"; }
      And to see this code in action:
      $scalar = get_list(); print $scalar, "\n"; @array = get_list(); print join(" ", @array), "\n";
      Results:
      SCALAR CALL
      5 10 15 20


      And similarly:
      print scalar(get_list()), "\n"; print join(" ", get_list()), "\n";
      Results:
      SCALAR CALL
      5 10 15 20


      Hopefully this helps.