top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Best way to return the list content in Perl?

0 votes
560 views
sub myfunc {
 my @x=(1,2,3);
 return @x;
}

or

sub myfunc {
 my @x=(1,2,3);
 return [@x];
}

which one is the better way to return the list content? And if the method is an instance method?

posted Aug 18, 2014 by Deepankar Dubey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Functionally, the two are identical. The first is returning a reference to the array you have created. The second is returning a reference to a new array created from a (shallow) copy of the array you have created.

The fist is to be preferred in my opinion, because it does exactly what you want. The second does extra work and might cause someone to wonder why you haven't just returned a reference to the array.

The second version is necessary when the array might persist between subroutine calls and you effectively need to return a copy.

answer Aug 18, 2014 by Kiran Kumar
Similar Questions
+2 votes

I have been writing a perl script that uses the Net::DNS modules. After banging my head, so to speak for many days, I asked on a DNS-related discussion list for help in figuring out why name server updates had started always failing with errors about not auth and BADKEY when I seem to recall that they had once worked. That was when someone told me that Net::DNS0.73 has a bug in it. The solution was to either downgrade to the previous version of Net:DNS or apply a release candidate which reportedly does not have the bug.

I didn't know for sure which version we had so I put a print statement in the code to cause it to print out the version number and sure enough, we have the buggy version.

Simply, what is the easiest way to downgrade so that we don't break anything else?

+1 vote

Is there a shorter way to write $a = ! $a, please?

Something analogous to ++ and -- operators like $a !! or !! $a would negate the variable $a and return its previous or new value respectively.

0 votes

As I understand, Perl has excellent report-generation capabilities. By using formats, we can actually visualize how our output will look because the definition of a format in Perl is very similar to what you see on the output. Is there any way we to convert these formats into HTML reports? My goal is to create good looking HTML reports. Please suggest/advice.

...