On 29/07/2011 23:57, Rajeev Prasad wrote:
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...

I think this thread is getting a little confused. The page you refer to
is very out of date, but in any case a little misguided.

The only thing you can pass to a subroutine is a LIST (by definition, a
list of scalar values). That list may have zero, one, or many elements,
but all of them are scalars. The list passed to a subroutine appears in
the built-in array @_ at run time.

Yes, it is possible to pass the data in an array to a subroutine by
writing mysub(@data), but that is not 'passing an array' as the array is
decomposed to a list of scalars before it is passed and has to be
rebuilt within the subroutine. That is, there is nothing we can do
'array things' with (aside from @_, and manipulating that is bad
practice) such as push, pop, splice etc unless we write

  my @params = @_;

It may be helpful here to read and understand

  perldoc "What is the difference between a list and an array"

The same thing happens with a hash. Sure, we can write mysub(%data) to
pass a list of scalars that is amenable to reforming back into a hash
within the subroutine, but it is not 'passing a hash'.

So the page is correct in that a subroutine's parameter list can only
contain scalar values, but there may be any number of them. They may be
passed in many different ways, but the subroutine's code will only ever
see a list of scalars. Even if a call looks like

  mysub(@p1, %p2, $p3, subp4())

the values are formed into a single list of scalar values, and presented
to the subroutine code in @_, where there is no distinction between the
source of the parameters.

I hope this clarifies things a little.

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to