in reply to passing arrays to subroutines
- call by value
- call by reference
References are always a good idea - especially if you have large or complex datastructures, but using values is much easier in the beginning. Here's an example that I've posted earlier this day on a german perl-community site. Two array-references gets passed to a function in an anonymous hash:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @numbers = (1..10); my @words = qw(hello world outside); function( {NUMBERS=>\@numbers , WORDS=>\@words} ); exit; sub function { die "no parameter!\n" unless @_; my %opt = %{ shift @_ }; print Dumper \%opt; my @i_words = $opt{WORDS}; print Dumper \@i_words; my @i_numbers = $opt{NUMBERS}; print Dumper \@i_numbers; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: passing arrays to subroutines
by QM (Parson) on Mar 16, 2005 at 14:36 UTC |