Anthanos has asked for the wisdom of the Perl Monks concerning the following question:

I am storing html in a MySQL table that contains variable names in it (i.e. $name $address etc). I would like to get the html from the table and output it with the appropriate data for each variable (local to the function) but instead it just outputs the text $name etc. How can i substitute variable values within a variable? i.e.:
$name = "My Name"; $sth = $dbh->prepare("SELECT html_code FROM html_table WHERE id LIKE ' +2'); $sth->execute; @row = $sth->fetchrow_array; $html = @row[0];
At this point I want to substitute $name for all instances of $name in $html... hope that makes sense. Thanks!

Replies are listed 'Best First'.
Re: Dynamic HTML via MySQL
by eg (Friar) on Feb 21, 2001 at 04:29 UTC
Re: Dynamic HTML via MySQL
by rpc (Monk) on Feb 21, 2001 at 04:39 UTC
    $wheel->reinvent();

    Take a look at HTML::Template. It's exactly what you're looking for, and its much more robust than rolling your own.

    --rpc
Re: Dynamic HTML via MySQL
by archon (Monk) on Feb 21, 2001 at 04:34 UTC
    Well first of all you want
    $html = $row[0]
    This will get the 0th element of the @row array.
    Then, if I am reading your request correctly, you do:
    $html =~ s/\$name/$name/g;

    Hope that helps.

      Thank you! the substitution works perfectly, and I am going to look into a more flexible templating solution.