Destructuring using rest parameters in Perl

126 Views Asked by At

I'm just getting started with Perl (5.38) and I recently learned how to destructure arguments from the argument array @_; for example: my ($var1, $var2) = @_;.

However, I wasn't able to find any information on using a rest parameter like we can in JavaScript, but it's entirely possible I was using search terms that were too specific to JS. For example, in JavaScript, you could do function (var1, ...rest). Is there an equivalent way of doing something like my ($var1, ...$rest) = @_; in Perl? I did see this seemingly related post In Perl, how can I unpack to several variables?, but that seems overly complicated for this simple use-case, and I couldn't tell from an initial skim whether it can even be used for this situation, since the example was using structs.

Note: I'm looking specifically for a rest parameter equivalent, I know I can achieve the same thing by using slices, for example, but that requires multiple assignments (unless I'm missing something).

1

There are 1 best solutions below

0
ikegami On BEST ANSWER
my ( $var1, @rest ) = @_;

It is also commonly written as follows:

my $var1 = shift;
my @rest = @_;