Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: Re: Dereference an array reference

by merlyn (Sage)
on Apr 19, 2001 at 02:54 UTC ( #73689=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Dereference an array reference
in thread Dereference an array reference

actually, methods (and subs in general) can return arrays
No, they can return lists in a list context, or scalars in a scalar context. Nothing else. Cannot return an array.

-- Randal L. Schwartz, Perl hacker

  • Comment on Re: Re: Re: Dereference an array reference

Replies are listed 'Best First'.
Re: Re: Re: Re: Dereference an array reference
by danger (Priest) on Apr 19, 2001 at 11:42 UTC

    That's all well and good to say -- but it might help to give a little context (Oh my, what pun!) to ponder:

    #!/usr/bin/perl -w use strict; my $scalar; my @list; sub foo { return (42, 24, 10); } sub bar { my @array = (42, 24, 10); return @array; } $scalar = foo(); # same as: $scalar = (42,24,10); @list = foo(); # same as: @list = (42,24,10); print "$scalar:@list\n"; # prints: 10:42 24 10 $scalar = bar(); # same as: $scalar = @array; @list = bar(); # same as: @list = @array; print "$scalar:@list\n"; # prints: 3:42 24 10 __END__
Sub cannot return an array?
by dvergin (Monsignor) on Apr 20, 2001 at 00:53 UTC
    [methods (and subs in general)] can return lists in a list context, or scalars in a scalar context. Nothing else. Cannot return an array.

    Okay, merlyn. I'm puzzled again. If anyone else had posted this assertion, I would have made this as a correction rather than a genuine inquiry. The following snippet seems to demonstrate the ret_array sub returning an array. But I've been enough rounds on the array/list thing to know that things ain't always what they seem. Is more happening here than meets the eye?

    sub ret_array { return @_; } sub ret_list { return @_[0..$#_]; } my @array = ('a','b','c'); print scalar ret_array('a','b','c'); # 3 print scalar ret_array(@array); # 3 print scalar ret_list('a','b','c'); # c print scalar ret_list(@array); # c

      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")
        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.

      The sub is always returning a scalar or a list. Never an array.

      The context of the caller is provided to the last expression evaluated in the subroutine. That expression then evaluates to a scalar or list, and that scalar or list is returned.

      In your ret_array, a scalar context evaluation of @_ is clearly the number of elements of the _ array, while a list context evaluation is the current contents of the _ array. But it's not "returning" the array. It's returning a copy of the contents (as a list), or the count (as a scalar).

      Similarly, for the ret_list subroutine, a scalar context is passed down to the subroutine to cause the last element of that slice to be returned (as a scalar), or in a list context, the entire contents of the _ array are returned (as a list).

      Again, at no time is the "array" returned. You're either returning the last expression evaluated in a list context as a list, or the last expression evaluated in a scalar context as a scalar.

      -- Randal L. Schwartz, Perl hacker

        Great! Very helpful. So if I have this right, the list/scalar context provided by the context in which the sub is called is being applied inside the subroutine at the point that the return expression (whether specified with a 'return' or not) is being evaluated. (Not applied to the result of the sub after it springs to life at the point the sub is called.) Yes?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-12-03 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (57 votes). Check out past polls.

    Notices?