Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are references better return values in Perl functions?

Tags:

perl

What are the pros and cons of returning an array or a hash compared to returning a reference on it?

Is there an impact on memory or execution time?

What are the functional differences between the two?

sub i_return_an_array
{
    my @a = ();
    # push things in @a;
    return @a;
}

sub i_return_a_ref
{
    my @a = ();
    # push things in @a;
    return \@a;
}

my @v = i_return_an_array();
my $v = i_return_a_ref();
like image 308
Alsciende Avatar asked Mar 15 '11 09:03

Alsciende


People also ask

Does Perl pass by value or reference?

Perl always passes by reference. It's just that sometimes the caller passes temporary scalars. Perl passes by reference. Specifically, Perl aliases each of the arguments to the elements of @_ .

What is the use of reference in Perl?

Perl Reference is a way to access the same data but with a different variable. A reference in Perl is a scalar data type which holds the location of another variable. Another variable can be scalar, hashes, arrays, function name etc.

What is return in Perl?

return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context.

How to dereference a hash reference in Perl?

Dereference a HASH First we print it out directly so you can see it is really a reference to a HASH. Then we print out the content using the standard Data::Dumper module. Then we de-reference it by putting a % sign in-front of it %$hr and copy the content to another variable called %h.


1 Answers

Yes, there is an impact on memory and execution time - returning a reference returns a single (relatively small) scalar and nothing else. Returning an array or hash as a list makes a shallow copy of the array/hash and returns that, which can take up substantial time to make the copy and memory to store the copy if the array/hash is large.

The functional difference between the two is simply a question of whether you work with the result as an array/hash or as an arrayref/hashref. Some people consider references much more cumbersome to work with; personally, I don't consider it a significant difference.

Another functional difference is that you can't return multiple arrays or hashes as lists (they get flattened into a single list), but you can return multiple references. When this comes up, it's a killer detail that forces you to use references, but my experience is that it only comes up very rarely, so I don't know how important I'd consider it to be overall.

Going to the title question, I believe that the most important factor regarding returning lists vs. references is that you should be consistent so that you don't have to waste your time remembering which functions return arrays/hashes and which return references. Given that references are better in some situations and, at least for me, references are never significantly worse, I choose to standardize on always returning arrays/hashes as references rather than as lists.

(You could also choose to standardize on using wantarray in your subs so that they'll return lists in list context and references in scalar context, but I tend to consider that to be a largely pointless over-complication.)

like image 164
Dave Sherohman Avatar answered Oct 08 '22 23:10

Dave Sherohman