Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Return value from function

by perlCrazy (Monk)
on Jul 12, 2007 at 17:00 UTC ( [id://626280]=perlquestion: print w/replies, xml ) Need Help??

perlCrazy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
From any subroutine what is the best and efficient way to return value ?

If I want to return hash or array, should I return ref of variable or just hash or array.
Till now I always used to return Ref, just want to know is this the efficient and best way to return value from function.

Replies are listed 'Best First'.
Re: Return value from function
by bart (Canon) on Jul 12, 2007 at 17:50 UTC
    Returning references is fast, but making use of the reference later is not always very practical, as you now must write $r->{$key} where otherwise you could have written $r{$key}.

    So often, I let the user decide, and base my return value on wantarray: If a sub is called in scalar context, I return a ref, but in list context, I return a flat list.

    sub foo { my %hash = ( a => 1, b => 2 ); return wantarray ? %hash : \%hash; }
    Call as either of:
    my $hashref = foo();
    or
    my %hash = foo();
Re: Return value from function
by runrig (Abbot) on Jul 12, 2007 at 17:18 UTC
    You can always Benchmark, but really just return whatever seems to make the most sense, or makes things more convenient. E.g., if you're using the result to pass to another function that accepts a reference, then pass back a reference. If you are returning two arrays, then you'll have to return references to them. If you know the arrays are huge, and this function might be called often, maybe pass back a reference anyway. Etc. I.e., don't worry about it too much.
Re: Return value from function
by bloonix (Monk) on Jul 12, 2007 at 17:35 UTC
    In most times it's better to pass references between functions, otherwise it would copy the data and if the hash or array is too big it blows your memory away. The other thing is that if you know that the function manipulate the data and you need the original then you should pass a copy.
      Thanks for reply.
      Is this the best option ? why not pass reference then copy locally instead of returning from function directly.
      How much difference this will make and what should be the best prcatice.
      Also if checking the retrun value from calling function, in case of Hash, if we do like that if (%hash), then this will
      not work in all case. If we return ref, then we can test easily like that, if ($ref). Any suggestions or opinion ?
Re: Return value from function
by lyklev (Pilgrim) on Jul 12, 2007 at 21:26 UTC
    If you are returning a simple, short list of values, or even a hash, returning the values is probably the best, because it is the simplest.

    However,

    my @values = f(...); # simple read, # @values is a copy
    or
    my %hash = f(...); # hash is also a copy my $value = $hash{'value'};
    makes a copy of all values, so if you return a lot of values, you might have some performance issues. In that cast, a reference might be faster.

    If you want to return more than one array or arrays and hashes where the returned lengths are unknown, references are your only choice:

    my (@v1, @v2) = f(...);
    makes @v1 slurp all return values, leaving @v2 empty. Solution, with a few comments:
    sub f { (...) return \@v1, \@v2, \%v3; # return 2 array and a hash # reference } my ($ar1, $ar2, $hr3) = f(); # collect references my @ar1 = @{ $ar1 }; # get the first array, # creating a duplicate foreach (@ {$ar2} ) {...} # avoid copying the array, # probably more efficient my $value = $hr3->{'value'} # some people find this # notation cumbersome # and avoid references

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found