in reply to reference question
Assuming both of them are called at the top of a subroutine ... my $myarray = shift; puts the first argument to the subroutine into the variable $myarray - no matter what the first argument is, it could be an array reference but it does not have to be. my $myarray = @_; This puts the number of arguments the subroutine was called with into $myarray - evaluating an array in scalar context.
To demonstrate passing an arrayref into a subroutine look at this:If you call it like this print_arr(@arr) then the $arr[0] will be put into $arrayref and an error occurs when perl tries to do @$arrayref because 'Beer' is not a valid reference. (Note that the function now is called with 3 arguments, @arr is flattened out!)sub print_arr { my $count = @_; my $arrayref = shift; print "I have been called with $count arguments.\n"; print "$_!!\n" for (@$arrayref) } my @arr = qw/Beer Wine PerlMonks/; print_arr(\@arr);
-- Hofmator
|
|---|