Is there a better way to pass by reference in Perl?

parameter-passing, parameters, pass-by-reference, perl, perl-module

Solution

Have you looked at Data::Alias? It lets you create lexically-scoped aliases with a clean syntax.

You can use it to create pass-by-reference semantics like this:

use strict;
use warnings;

use Data::Alias;

sub foo {
    alias my ($arg) = @_;
    $arg++;
}

my $count = 0;

foo($count);

print "$count\n";

The output is `1`, indicating that the call to `foo` modified its argument.

Problem

I am doing pass-by-reference like this: ``` use strict; use warnings; sub repl { local *line = \$_[0]; our $line; $line = "new value"; } sub doRepl { my ($replFunc) = @_; my $foo = "old value"; $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 me 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.

Original source