How can I pass the elements in a Perl array reference as separate arguments to a subroutine?

dereference, perl, reference

Solution

This should do it:

foo(@$args)

That is not actually an `apply` function. That syntax just dereferences an array reference back to plain array. man perlref tells you more about referecences.

Problem

I have a list that contains arguments I want to pass to a function. How do I call that function? For example, imagine I had this function: ``` sub foo { my ($arg0, $arg1, $arg2) = @_; print "$arg0 $arg1 $arg2\n"; } ``` And let's say I have: ``` my $args = [ "la", "di", "da" ]; ``` How do I call `foo` without writing `foo($$args[0], $$args[1], $$args[2])`?

Original source