Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^5: Evil Interview Questions (memes) (return @foo VS return &foo)

by lodin (Hermit)
on Feb 09, 2008 at 21:00 UTC ( [id://667204]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Evil Interview Questions (memes)
in thread Evil Interview Questions

That said, it seems blindingly obvious that return @this returns an array!

Is it blindly obvious--really? Would you also say that return &foo obviously returns a subroutine?

return @array doesn't return an array any more than return &foo returns a subroutine. The key is that return EXPR doesn't make the subroutine/eval/do FILE return the expression itself, but rather the evaluation of the expression, which may depend on the context in which the subroutine/eval/do FILE is evaluated.

But would we say that an array is a list or a scalar?

You're focusing on what an array is in different contexts. An array is an array. If you focus on what an array evaluates to you'll get more to the point. Every evaluation is in a context, and an array evaluates differently in list versus scalar context just as a subroutine (may) do. perldata says (my emphasis)

Note that the value of an actual array in scalar context is the length of the array
i.e., it evaluates to the length.

lodin

Replies are listed 'Best First'.
Re^6: Evil Interview Questions (memes) (return @foo VS return &foo)
by kyle (Abbot) on Feb 10, 2008 at 01:22 UTC

    Excellent! I think that finally explains it.

    If you focus on what an array evaluates to you'll get more to the point.

    Whatever expression return will be evaluated in the context of the sub's call (as described by wantarray), and passed out that way.

    Would you also say that return &foo obviously returns a subroutine?

    No, but I don't consider "&foo" to be a passable data structure. The only time it isn't a call to a sub is (1) if it has "\" in front of it (making a reference), (2) if it has defined or exists in front of it, and (3) some other surprise I haven't discovered yet.

    sub foo { die 'call to foo' } print 'foo is ', defined &foo ? 'defined' : 'not defined', "\n"; print 'bar is ', defined &bar ? 'defined' : 'not defined', "\n"; __END__ foo is defined bar is not defined

    Many thanks for your reply. I do believe you have penetrated my concrete cranium.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://667204]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-03-28 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found