Is returning a whole array from a Perl subroutine inefficient?

后端 未结 8 1869
野性不改
野性不改 2020-12-09 10:41

I often have a subroutine in Perl that fills an array with some information. Since I\'m also used to hacking in C++, I find myself often do it like this in Perl, using refer

8条回答
  •  长情又很酷
    2020-12-09 11:09

    If I look at your example and think about what you want to do I'm used to write it in this manner:

    sub getInfo {
      my @array;
      push @array, 'obama';
      # ...
      return \@array;
    }
    

    It seems to me as straightforward version when I need return large amount of data. There is not need to allocate array outside sub as you written in your first code snippet because my do it for you. Anyway you should not do premature optimization as Leon Timmermans suggest.

提交回复
热议问题