Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: More efficient dereferencing of a subroutine return

by gg48gg (Sexton)
on Feb 19, 2013 at 17:51 UTC ( [id://1019630]=note: print w/replies, xml ) Need Help??


in reply to Re^2: More efficient dereferencing of a subroutine return
in thread More efficient dereferencing of a subroutine return

Hey all. Related question. Is there a way to avoid this multi step operation when I have multiple return values of a sub? e.g.:
my ($ref1, $ref2) = retarrayrefs(); my @arr1=@$ref1; my @arr2=@$ref2; sub retarrayrefs { my @arr1=qw(one two three); my @arr2=qw(four five six seven); return (\@arr1,\@arr2); }
I was trying many variations of the below, but none work properly, and it just doesn't seem like a good thing to do. Should I stop trying to shorten this code and simply stick with the above?
my (@arr1,@arr2) = ( ${retarrays()[0]}, ${retarrays()[1]} );

Replies are listed 'Best First'.
Re^4: More efficient dereferencing of a subroutine return
by BrowserUk (Patriarch) on Feb 19, 2013 at 18:22 UTC
    Is there a way to avoid this multi step operation when I have multiple return values of a sub?

    Yes:

    sub fillArrays { my( $r1, $r2 ) = @_; my @$r1 = qw(one two three); my @$r2 = qw(four five six seven); return; } my (@arr1,@arr2); fillArrays( \@arr1, \@arr2 );

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      This also works and is 'shorter', although I would not say more maintainable:

      >perl -wMstrict -MData::Dump -le "sub fillArrays { my( $r1, $r2 ) = @_; @$r1 = qw(one two three); @$r2 = qw(four five six seven); } ;; fillArrays( \my (@arr1, @arr2) ); ;; dd \@arr1, \@arr2; " (["one", "two", "three"], ["four", "five", "six", "seven"])
        Thanks to all of you. I am going to stick to the "keep it simple, stupid" philosophy even though it is more lines and typing. I appreciate everyones' replies.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found