better way to pass by reference in Perl?

Posted by JoelFan on Stack Overflow See other posts from Stack Overflow or by JoelFan
Published on 2010-03-17T18:06:43Z Indexed on 2010/03/17 18:11 UTC
Read the original article Hit count: 494

Filed under:
|

I am doing pass-by-reference like this:

sub repl {
    local *line = \$_[0]; our $line;
    $line = "new value";
}

sub doRepl {
    my $foo = "old value";
    my ($replFunc) = @_;
    $replFunc->($foo);
    print $foo; # prints "new value";
}

doRepl(\&repl);

Is there a cleaner way of doing it?

Prototypes don't work because I'm using a function reference (trust be that there's a good reason for using a function reference).

I also don't want to use $_[0] everywhere in repl because it's ugly.

© Stack Overflow or respective owner

Related posts about perl

Related posts about pass-by-reference