ultranerds has asked for the wisdom of the Perl Monks concerning the following question:
...but instead of using $_[0], I wanna use a variable name. I know we can do this with hashrefs and arrayrefs using: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; }
..and: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"; }
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)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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Way to do a "scalar ref"?
by egga (Monk) on Sep 26, 2011 at 08:58 UTC | |
by ultranerds (Hermit) on Sep 26, 2011 at 08:59 UTC | |
|
Re: Way to do a "scalar ref"?
by davido (Cardinal) on Sep 26, 2011 at 08:46 UTC | |
by ultranerds (Hermit) on Sep 26, 2011 at 08:51 UTC | |
by moritz (Cardinal) on Sep 26, 2011 at 09:02 UTC | |
by johngg (Canon) on Sep 26, 2011 at 09:13 UTC | |
by AnomalousMonk (Archbishop) on Sep 26, 2011 at 18:45 UTC | |
|
Re: Way to do a "scalar ref"?
by Jenda (Abbot) on Sep 26, 2011 at 09:42 UTC | |
|
Re: Way to do a "scalar ref"?
by doug (Pilgrim) on Sep 26, 2011 at 17:14 UTC | |
|
Re: Way to do a "scalar ref"?
by ikegami (Patriarch) on Sep 26, 2011 at 17:54 UTC |