Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Syntactic Confectionery Delight
 
PerlMonks  

Re: More efficient dereferencing of a subroutine return

by tobyink (Abbot)
on Feb 17, 2013 at 22:54 UTC ( #1019228=note: print w/ replies, xml ) Need Help??


in reply to More efficient dereferencing of a subroutine return

my @ARRAY = @{ subroutine() };

This is fewer lines of course, and arguably a bit neater than a temporary variable, but in terms of real efficiency (i.e. CPU, memory, etc) makes very little difference.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: More efficient dereferencing of a subroutine return
Download Code
Re^2: More efficient dereferencing of a subroutine return
by parv (Priest) on Feb 17, 2013 at 23:18 UTC
    Darn thing causes fatal error of Can't use an undefined value as an ARRAY reference if undef is received under strict 'refs', where the dereference result could have been an empty list without the need to invoke no strict ... . I will take the temporary variable to do a check first.
      what? Can you demonstrate that?
      Can you show what exactly gives you that error? I get no errors/warnings with this:
      #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my @result = @{ subroutine() }; say "@result"; sub subroutine { my @array = (); return \@array; }

        Did you, and the one before you, missed the part where I wrote about the case of undef being returned, as in ...

        use strict; my @p = @{ list() }; sub list { return undef; }

        ...?

      In that case,

      my @ARRAY = @{ subroutine() || [] };
      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re^2: More efficient dereferencing of a subroutine return
by gg48gg (Sexton) on Feb 18, 2013 at 17:43 UTC

    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.
      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-04-20 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls