If I understand you correctly, you are saying that in your MySQL table there are some rows with character data, and included in that character data are the names of variables, which you wish to substitue for at runtime. The easiest way to do this is eval, but please don't use it because it is extremely dangerous. Instead, you should keep the variables you want to substitue for not as Perl variables, but as entries in a hash. So for example, earlier in your program you might have code like

my %table; $table{type_of_animal} = "giraffe";
then you would do the substitution as something like
my $row = $sth->fetchrow_arrayref; $row->[0] =~ s/\$(\w+)/exists $table{$1} ? $table{$1} : "\$$1"/eg;
If you really must use variables instead of a hash, then do it with symbolic references:
{ no strict 'refs'; my $row = $sth->fetchrow_arrayref; $row->[0] =~ s/\$(\w+)/$$1/eg; }
But then remember that your variable needs to be a global, not a lexical, in that case. Either of these ways is definitely better than eval, though.

Update: Actually, if you use sh1tn's method I think the eval is safe. But otherwise it is not.


In reply to Re: Interpreting variables pulled from MySQL by Errto
in thread Interpreting variables pulled from MySQL by cdherold

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.