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

Thanks to everyone for your input. For my purposes, I don't have to worry about the possibility of my sub returning undef. So, I like tobyink's:

my @ARRAY = @{ subroutine() };
for it's simplicity and function. I will keep in mind Anon's recommendation for using 'eval' in the event I need to handle possibility of undef. Thanks all! I hope this thread is useful for others in the future. I didn't find what I was looking for when I searched. Cheers.

Replies are listed 'Best First'.
Re^3: More efficient dereferencing of a subroutine return
by gg48gg (Sexton) on Feb 19, 2013 at 17:51 UTC
    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]} );
      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"])