I have a list of variable names that I want to loop through and do stuff to the actual variable.

use strict; my @varlist = ("foo", "bar", "baz"); my ($foo, $bar, $baz) = ("1", "2", "3"); my $newfoo = "A"; foreach my $var (@varlist) { my $newvarname = "new" . $var; if (defined $$newvarname) { $$var = $$newvarname; } }

Of course I get the error "Can't use string ("newfoo") as a SCALAR ref while "strict refs" in use"

Searching I found can't use string as a SCALAR ref while strict refs which says to use a hard reference. However I have no clue how to do that in the foreach loop.

Further down in that node (and in other search results) suggest using a hash. I really don't see how to do that in my case.

What I'm trying to do is write a script that will update values in one of our app's config file. I just don't want to have to type a million lines of code to test each value to see if it should be updated or not. The above is what I came up with. If I can make that work, great. If there's a better way, let me know that too.

Thanks in advance


In reply to using a string as a SCALAR ref while "strict refs" in use by xorl

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.