Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,
how to pass the local variabls of one subroutine as reference to other subroutines.
i would like to make modification on the local variabls in some other subroutines.
If possible to do that modification in other subroutines , is it safe??

Replies are listed 'Best First'.
Re: passing arguments as reference
by lidden (Curate) on Apr 14, 2006 at 10:52 UTC
    It is possible.
    use strict; use warnings; sub foo{ my $a = 1; print "$a\n"; bar(\$a); print "$a\n"; } sub bar{ my $b = shift; $$b = 2; } foo;
      Avoid using $a and $b as sample variables because of their use as package global variables by sort
Re: passing arguments as reference
by davidrw (Prior) on Apr 14, 2006 at 10:57 UTC
    What kind of variables? If just plain out scalars (number/string), then you have to pass a reference. Same for an array or hash. If you have an array ref or hash ref (or object), it's already a reference.
    use Data::Dumper; foo(); sub foo { my $x = 3; my @y = ( 1 .. 3 ); my $yy = [ 1 .. 3 ]; my %z = ( a => 1, b => 2 ); my $zz = { a => 1, b => 2 }; my $obj = My::Obj->new; # e.g. bless'd hashref print Dumper [ $x, \@y, $yy, \%z, $zz, $obj ]; bar( \$x, \@y, $yy, \%z, $zz, $obj ); print Dumper [ $x, \@y, $yy, \%z, $zz, $obj ]; } sub bar { my @args = @_; ${$args[0]} += 2; # add 2 to x $args[1]->[1]++; # 2->3 in y $args[2]->[1]++; # 2->3 in yy $args[3]->{a}++; # 1 -> 2 in z $args[4]->{a}++; # 1 -> 2 in zz }
    You can also do this:
    $x = 4; warn $x; blah($x); warn $x; sub blah { $_[0] += 3; }
Re: passing arguments as reference
by Roy Johnson (Monsignor) on Apr 14, 2006 at 14:46 UTC
    By default, Perl passes arguments by reference (or, technically, by alias). If you modify the elements of the @_, you will modify the variables that were passed in:
    use strict; use warnings; sub modify_args { $_[0] = 'new-value'; } my ($one, $two) = qw(cat dog); modify_args($one, $two); print "$one $two\n";
    Of course, you can also pass references instead of the variables themselves, and then unload @_ in the usual way so that you don't have to work with that undescriptively-named array. Or you can take references to the elements of the array:
    sub modify_args1 { my ($argref1, $argref2) = \(@_[0..$#_]); $$argref1 = 'new value'; }

    Caution: Contents may have been coded under pressure.