środa, 15 grudnia 2010

Passing array as a parameter to Perl subroutine

In Perl if you pass an array as a parameter to a subroutine like this: 
someroutine(@arg1, $arg2);
Then inside the routine you would expect the following to be the way to get the parameter values:
(@arg1in, $arg2in) = @_;
but that doesn't work as the @arg1in get assigned the entire content of the @_; A solution to that is passing an array reference instead so the call would look like that:
someroutine(\@arg1, $arg2);
Then inside after doing this:
($arg1in, $arg2in) = @_;
you can refer to the array as @{$arg1in}  or to its elements like ${$arg1in}[1].
See also: http://www.cs.cf.ac.uk/Dave/PERL/node61.html

Brak komentarzy:

Prześlij komentarz