Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

(tye)Re: Sub cannot return an array?

by tye (Sage)
on Apr 20, 2001 at 01:00 UTC ( [id://73993]=note: print w/replies, xml ) Need Help??


in reply to Sub cannot return an array?
in thread Dereference an array reference

Yes, you are suffering from the all too common "arrays in a scalar context return their size while lists in a scalar context return their last member" syndrome.

If ret_array were returning an array, then you could do array things to it like: push( ret_array(@a), "add" ); which you can't.

So you don't call something an array based on what it returns in a scalar context. The definition of an array in Perl is much narrower than that (an array is a type of variable, not a type of value).

The flip side of this is that there are lots of definitions for "list" going around so you have to be careful to realize which one is being used each time you see the word "list". Feel free to search for more on this topic (I've said plenty on it recently and not so recently). (:

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Sub cannot return an array?
by dvergin (Monsignor) on Apr 20, 2001 at 01:08 UTC
    tye, excellent. Thanks. (You may recognize the example as one I cribbed and modified from you in one of the previous discussions on this topic.) This topic has become a bit of an obsession and I have indeed read a generous sampling of the previous postings.

    So far, so good. So how do we put into words the difference in behavior in the two subs in the example? Update: merlyn provides the response to this question in his reply.

      "They return different values when called in a scalar context." ?

      You could make versions that return the first element or a reference to an array or that die when called in a scalar context. But they all return he same list in a list context.

      Trying to come up with some shorthand that describes both what they return in a list context and what they return in a scalar context without ever mentioning "context" is just a mistake in my book.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found