I have a function (let's call it foo($array_reference, ...)
) that expects an array reference among other parameters. I want to have foo
shift the array reference off the list of parameters passed to it directly to an array, without having to shift it off as an array reference and then separately convert that to an array.
What I want should be something like:
my @bar = @{shift};
What I do not want, but am currently stuck with:
my $bar = shift;
my @bar = @{$bar}
The latter approach wastes lines, wastes memory, and causes me to hate the writer of this type of Perl code with a fiery passion. Help, please?
Create a temp variable and assign the value of the original position to it. Now, assign the value in the new position to original position. Finally, assign the value in the temp to the new position.
shift() The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
Do not worry about "wastes lines, wastes memory." Both lines of code and memory are cheap.
You can operate on @_
just like any array, and that includes dereferencing. It sounds like you want one of:
my @bar = @{+shift};
my @bar = @{$_[0]};
Try my @bar = @{shift()};
or my @bar = @{CORE::shift()};
Perl will warn you that @{shift}
is ambigous if you enable warnings with use warnings;
.
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