I know I can create an array and a reference to an array as follows:
my @arr = ();
my $rarr = \@arr;
I can then iterate over the array reference as follows:
foreach my $i (@{$rarr}){
}
Is there a way to copy or convert the array ref to a normal array so I can return it from a function? (Ideally without using that foreach loop and a push).
You have the answer in your question :-)
use warnings;
use strict;
sub foo() {
my @arr = ();
push @arr, "hello", ", ", "world", "\n";
my $arf = \@arr;
return @{$arf}; # <- here
}
my @bar = foo();
map { print; } (@bar);
Like this:
return @{$reference};
You're then just returning a dereferenced reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With