
davorg
Thaumaturge
/ Moderator
Sep 16, 2002, 12:09 PM
Post #14 of 16
(5840 views)
|
Re: [fashimpaur] Pass array to function.
[In reply to]
|
Can't Post
|
|
Dave, However, in the "Programming Perl" book from O'Reilly: Any backslashed prototype character ... represents an actual argument that absolutely must start with that character. Thus, you are not forcing it to be an array reference. It is just saying that that prototype is saying it must have an array as the second argument. Just a japh opinion, Dennis, I agree completely that the \@ notation forces the argument to begin with a @. But that doesn't preclude it being turned into a reference as well. I admit that the docs and the Camel are both unclear on this point, but p230 of the Camel (3ed) says:
If you're writing new code and would like a unary operator that takes only a scalar reference, not any old scalar expression, you could prototype it to take a scalar reference. sub func (\$) { my $nref = shift; print "you gave me $$nref\n"; } I'm assuming that the same will apply to arrays and hashes. Anyway, if an array isn't turned into a reference, how would you explain this behaviour:
#!/usr/bin/perl use warnings; use strict; sub array (\@) { print "The subroutine has ", scalar @_, " arguments\n"; print "The first argument is "; my $ref = ref $_[0]; if ($ref) { print "a reference to a $ref\n"; } else { print "not a reference\n"; } } my @array = 1 .. 10; array(@array); On my system, this prints:
The subroutine has 1 arguments The first argument is a reference to a ARRAY -- Dave Cross, Perl Hacker, Trainer and Writer http://www.dave.org.uk/ Get more help at Perl Monks
|