E.Cross E.Cross - 1 year ago 95
Perl Question

Pass array and scalar to a Perl subroutine

Possible Duplicate:
How do pass one array and one string as arguments to a function?

I have a function, or subroutine, that takes in the first parameter as an array and the second parameter as a scalar. For example,

sub calc {
my @array = $_[0];
my $scalar = $_[1];
print @array, $scalar;

The problem is that the function is making the array equal to the first value of the array passed in, and the scalar to be the second value of the array passed in. When I want the first array to be the entire array, I do not need to make a deep copy of the array. For example,

my @array = ('51', 'M');
my $scalar = 21;


calc(@array, $scalar)

will print
51 M
when I want
51 M 21


You need to pass it in as a reference:

calc(\@array, $scalar)

And then access it as: my @array = @{$_[0]};