Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Destructuring using rest parameters in Perl

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

my ( $var1, @rest ) = @_;

It is also commonly written as follows:

my $var1 = shift;
my @rest = @_;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading