Barry Brevik wrote:
> I have a situation where I specifically want to pass the name of an
> array to a subroutine, and allow that subroutine to modify values in the
> caller's copy of the array.
my @arr = eval '@' . $name;
Though PBP says it's bad, because it's compiling during eval so it takes time,
and also because it's harder to find
problems and even comprehend the code.
> Ideally, the subroutine will never have a copy of the array in it's own
> space, but will only operate on the array in the caller. Can anyone show
> me how this is done?
I'd use hash with name keys and arrayref values, like $array{$name} = [1, 2, 3];
Then you can pass \%array and $name to your subroutine.
About caller's copy: if you pass a reference to sub, it's always the same
reference, and modifying some values will
modify "original" data.
A small example:
___CUT___
#!/usr/bin/perl -w
use strict;
use warnings;
my @arr1 = qw/a b c/;
my @arr2 = qw/d e f/;
print_arr('arr1');
print_arr('arr2');
ch_r(\...@arr1);
print_arr('arr1');
sub print_arr {
my ($name) = @_;
my @arr = eval "@" . $name;
print join(', ', @arr), $/;
}
sub ch_r {
my ($ref) = @_;
$ref->[1] = 'x';
}
--
Serguei Trouchelle
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs