http://www.perlmonks.org?node_id=927816

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

Hi,

I'm trying to make a bit of code (to save on memory, and unrequired "returns" etc), so that I can edit the value directly. Kinda like so:
my $test = "foo bar"; print "Test is now: $test \n"; test($test); print "Test is now: $test \n"; sub test { $_[0] =~ s/foo/whatever/g; }
...but instead of using $_[0], I wanna use a variable name. I know we can do this with hashrefs and arrayrefs using:
my @test_array = qw/foo bar test/; print Dumper(@test_array); test_array(\@test_array); print Dumper(@test_array); sub test_array { my $array_ref = $_[0]; $array_ref->[1] ="whatever"; }
..and:
my %test_array; $test_array{foo} = 1; $test_array{bar} = 2; $test_array{test} = 3; print Dumper(%test_array); test_hash(\%test_array); print Dumper(%test_array); sub test_hash { my $array_ref = $_[0]; $array_ref->{foo} = "whatever"; }
But I can't work out how to do it with a string, using a proper string name? (instead of $_[0], which will make it confusing when coming back to the code)

TIA!

Andy