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

In reply to Way to do a "scalar ref"? by ultranerds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.