There are several ways to do this. Pass the scalar link to $foo explicitly or use the Perl built-in pass on link semantics.
Explicit Link:
my $foo = "old value"; doRepl( \&repl, \$foo ); print $foo;
Follow this link:
my $foo = "old value"; doRepl( \&repl, $foo ); print $foo;
Even an amateur follows the link:
my $foo = "old value"; doRepl( \&repl, $foo ); print $foo;
The first uses regular perl hard links to do the job.
The first pass through the ref method takes advantage of the fact that Perl passes arguments to all functions as references. The @_ elements are actually aliases for the values ββin the argument list when calling the subroutine. By changing $_[0] to foo() , you will actually change the first argument to foo() .
The second pass by the ref method takes advantage of the fact that sub, called with & sigil and no parens, gets the @_ array of its caller. Otherwise, it is identical.
Update: I just noticed that you want to avoid $_[0] . You can do this in repl if you want:
sub repl { for my $line( $_[0] ) { $line = 'new value'; } }
daotoad
source share