[Brisbane-pm] Passing a Two-Dimensional Array to a sub in Perl 5.8.6

Robert Loomans robertl at apnic.net
Wed Feb 21 18:59:13 PST 2007


> However, I strongly suggest that even though Perl has a prototype  
> mechanism that can be used to enforce passing by reference, not using  
> it.

Seconded. Unless you have a strong reason, don't use prototypes at all,
and only use the array ref prototype under duress.

> So:
> 
>    sub Rain_calculation {
>       my @stuff = @_;
>       my @Rain_series = @{ $stuff[5] };

Personally, if I'm using multiple args in a sub, I do something like:

sub Rain_calculation {
    my ($zero, $one, $two, $three, $four, $five) = @_;

    do_something($five->[0]);

    foreach my $elem (@$five) {

(where $zero ... $five would have more meaningful names).

That way you aren't dealing with meaningless references to $_[x] that
make the code really difficult to read, and the array you passed in via
$five is not copied....

If you really must use $five as a real array you can just do:

    my @big_five = @$five;

... but realise that @big_five is then only a copy of the first level of
your structure; the elements will be references to the same arrays you
created in @Rain_series.

Cheers,
    Rob

-- 
Robert Loomans                                 Email:  robertl at apnic.net
Programmer/Analyst, APNIC                      Phone:    +61 7 3858 3100
http://www.apnic.net                             Fax:    +61 7 3858 3199
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3818 bytes
Desc: S/MIME Cryptographic Signature
Url : http://mail.pm.org/pipermail/brisbane-pm/attachments/20070222/a3d9d525/attachment-0001.bin 


More information about the Brisbane-pm mailing list