On 7/29/11 Fri Jul 29, 2011 3:57 PM, "Rajeev Prasad" <rp.ne...@yahoo.com> scribbled:
> Hello, > > from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm > > i found: > > In Perl, you can pass only one kind of argument to a subroutine: a scalar. > To pass any other kind of argument, you need to convert it to a scalar. You > do that by passing a reference to it. A reference to anything is a > scalar. If you're a C programmer you can think of a reference as a pointer > (sort of). > > is that still true? date on website is 2003... Yes, that is mostly true. You can pass an array to a subroutine, call_my_sub(@array); but an array is just a list (ordered set) of scalars. The contents of the array are placed into the local @_ array for you within the subroutine. If you try to pass two arrays: call_my_sub( @array1, @array2 ); they end up as a single list in the @_ array. If you pass more than one scalar: call_my_sub( $x, $y, $z ); they also end up as elements of the @_ array. So you could also make the statement: "You can only pass an array (list) to a Perl subroutine", but it is better to understand how Perl passes arguments to subroutines and avoid making sweeping statements like that. The situation is made more complicated by the use of prototypes, but I don't use them, don't understand everything about them, so won't go into that subject. If you really want to pass two arrays to a subroutine, you have to use references: call_my_sub( \@array1, \@array2 ); sub call_my_sub { my( $aref1, $aref2 ) = @_; my @local1 = @$aref1; my @local2 = @$aref2; } A similar mechanism is used to return values, either scalars or arrays, back to the calling routine. See 'perldoc perlsub' for more information about Perl subroutines. See 'perldoc perlref' and 'perldoc perlreftut' for information -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/