Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

More efficient dereferencing of a subroutine return

by gg48gg (Sexton)
on Feb 17, 2013 at 22:46 UTC ( #1019227=perlquestion: print w/ replies, xml ) Need Help??
gg48gg has asked for the wisdom of the Perl Monks concerning the following question:

I am inquiring to see if there is a more efficient way, than the below code, to dereference a reference returned from a subroutine. Could you please help me out?

my $temp_scalar=subroutine(); my @ARRAY=@$temp_scalar; undef $temp_scalar; sub subroutine { my @array=("one", "two"," three"); return \@array; }

Comment on More efficient dereferencing of a subroutine return
Download Code
Re: More efficient dereferencing of a subroutine return
by tobyink (Abbot) on Feb 17, 2013 at 22:54 UTC
    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
      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; }

        In that case,

        my @ARRAY = @{ subroutine() || [] };
        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      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]} );
Re: More efficient dereferencing of a subroutine return
by bulk88 (Priest) on Feb 18, 2013 at 02:16 UTC
    Don't make a copy of the array to another array. no "my @ARRAY=@$temp_scalar;" just do $temp_scalar->[5].
Re: More efficient dereferencing of a subroutine return
by LanX (Canon) on Feb 18, 2013 at 02:40 UTC
    Are you aware that Perl has list-context?

    my @ARRAY=subroutine(); sub subroutine { my @array=("one", "two"," three"); return @array; }

    If you still need to return an array-ref in other cases just check wantarray in a condition to see if a scalar is expected.

    return wantarray ? @array : \@array;

    Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-09-30 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (363 votes), past polls