there - just started learning Perl.
This is what I'm doing to get an array into a subfunction - can it be done simpler in 1 line?
sub my_sub {
my $ref_array = shift;
my @array = @$ref_array;
}
there - just started learning Perl.
This is what I'm doing to get an array into a subfunction - can it be done simpler in 1 line?
sub my_sub {
my $ref_array = shift;
my @array = @$ref_array;
}
If you want the effect of the shift
as well,
sub my_sub {
my @array = @{+shift};
}
The unary +
operator forces shift
to be treated as an expression, not a variable name. (Otherwise @{shift}
means the same as @shift
.)
Another approach is to not worry about it being an arrayref -- just leave it that way and use it in the rest of your sub as-is.
You could simplify just as
sub my_sub {
my @array = @{$_[0]};
}
Where @_
is the default array/list, used in parameter passing.
This is a heck of a lot cheaper:
local *array = shift();