TheFirstPerl TheFirstPerl - 4 years ago 186
Perl Question

Perl Dynamic Parameter to Function

I've got one function which I can't modify and in this function something like this is done

foreach my $param (@_) {
.....


Now I want to call this function with dynamic parameters.
I've got an object which I get from

$self->{'parameter'} = from_json ( $self->{'cgi'}->param('POSTDATA') );


And now I want to do something like this

function(para1, $self->{'parameter'})


if I do it manually it works

function(para1, 1, 1, 1)


How can I do that?

mob mob
Answer Source

If $self->{parameter} is an array reference, then you can include the dereferenced array and in most* cases, Perl will flatten the list.

$self = from_json ...;                  # $self->{parameter} = [1,2,3];
function($p1,@{$self->{parameter}});    # equiv to function($p1,1,2,3)

* - some cases where Perl won't flatten a list are functions that have a @ prototype or a Perl builtin function that expects an ARRAY and not just a LIST

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download