in reply to Interpreting variables pulled from MySQL
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
then you would do the substitution as something likemy %table; $table{type_of_animal} = "giraffe";
If you really must use variables instead of a hash, then do it with symbolic references:my $row = $sth->fetchrow_arrayref; $row->[0] =~ s/\$(\w+)/exists $table{$1} ? $table{$1} : "\$$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.{ no strict 'refs'; my $row = $sth->fetchrow_arrayref; $row->[0] =~ s/\$(\w+)/$$1/eg; }
Update: Actually, if you use sh1tn's method I think the eval is safe. But otherwise it is not.
|
|---|