Perl get link to temp list returned by function without making a copy?

I have a function in perl that returns a list. I understand that when foo () is assigned a list, it is copied:

sub foo() { return `ping 127.0.0.1` }

my @list = foo();

Then @listyou need to transfer to another list, for example @oldlist = @list;, and another copy is made. So I was thinking if I could just make a link from the returned list, for example my $listref = \foo();, and then I can assign this link, but that will not work.

The function I'm working with runs a command that returns a fairly large list (the ping command is for example purposes only), and I call often, so I want to minimize copies if possible. What is a good way to handle this?

+1
source share
1 answer

my $listref = [ foo() ];

arrayref ? , .


, " "... ; ( )

, .

"" ( \&sub_name)

\ ,

my @ref_of_LIST = \( 1,2,3 );  #-->  @ref_of_LIST: (\1, \2, \3)

, , . , .

my $ref_of_LIST = \( 1,2,3 );  #--> $ref_of_LIST: \3

, , \foo().


. ( ),

+6

Source: https://habr.com/ru/post/1689291/


All Articles