laziness, impatience, and hubris | |
PerlMonks |
Re^2: Evil Interview Questionsby kyle (Abbot) |
on Feb 09, 2008 at 04:08 UTC ( [id://667133]=note: print w/replies, xml ) | Need Help?? |
Can you show an example of a function that returns an array? While you haven't said so, I'm guessing that what you have on your mind is something like what you said in Re^4: returning tied array, which is to say: You can only return scalars and lists and (nothing) from subroutines. You can't return arrays or hashes directly, only as lists or references. I suspect this statement is more meaningful to perl programmers than to Perl programmers. I haven't read perlguts, let alone perl source, but I'm guessing that under the covers somewhere, in the sea of C, it's really true that nothing can escape a sub besides a scalar, a list, or Nothing. That having been said, please consider:
get_list and get_array return the same collection of values in different ways. They do different things in scalar context. This is why I think there's a difference between returning a list and returning an array—they behave differently. One might say that they both do the same thing in scalar context—return a scalar (neither list, nor array). I think these points of view are looking at the sub from different sides of the return. This is the difference between "imply" and "infer". It's the difference between what one says and what another hears. It's the difference between expression and interpretation. If I scream "value" into the Void, have I still said something? I think one could correctly say that sub { @_ } returns an array. It always returns an array regardless of context. Its caller may receive from it a scalar or a list or Nothing, depending on the context it provides, but what the sub itself does is the same every time. I have in my head a call like this:
Inside get_stuff somewhere there's a return with some "stuff" after it. I'm guessing that perl takes that "stuff" and turns it into something that makes sense to whatever "..." is, and it does that before the "stuff" gets out of get_stuff. So what actually comes out of the sub can only be a scalar, a list, or Nothing. I could conceive of it being implemented differently. It could pass out of the sub whatever the sub "said" it wanted to return, and then coerce it into the appropriate type once it got there. If that were the case, would we still say that subs can only return scalars, lists, or Nothing? Would they really behave any differently? More to the point, how is any of this distinction relevant to Perl programmers?
In Section
Meditations
|
|