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

Hello Monks,

I have a series of dynamic web pages that have (a lot) of common code. I've decided that one way to make sure any updates to said code are propagated across the entire site is to store some of the code inside the database itself.

One of the things that is stored is the HTML header for every page, which, aside from the page name, is the same. The page name itself doesn't change, but is based on another variable stored somewhere else.

I've got the whole header stored in the database, in a LONGTEXT (I believe) type field (I'll verify and post if it's different - I'm not in front of the thing at the moment). There is ALSO a perl variable stored in the header (in the database) - $config{'pagename'} - and when this information gets pulled from the db and printed, it somehow does not get interpreted by perl, and prints EXACTLY as the variable name (without being replaced - e.g. $config{'pagename'}).

I inserted the header information into the appropriate field in the database using phpmyadmin - I copied it directly from working code in my script.

Can anyone direct me as to why it is that I'm getting "escaped" variables in the output? Do I need to escape the variable name BEFORE it goes into the database, so that perl can interpret it properly when it gets read and printed?

ps I'm using the Mysql perl module... perhaps obviously.

pps here's a sample (but not exactly), just to give you the idea of what I'm talking about. The following would be stored in the database field:

<HTML> <HEAD> <title>$config{'pagename'}</title> ... some other header information, such as javascripts ... </HEAD> <BODY>

And when it prints out, the $config{'pagename'} is printed exactly as that without interpretation...

Thanks in advance!

-r-

Replies are listed 'Best First'.
Re: Perl variable names stored in mysql fields
by GrandFather (Saint) on Nov 12, 2009 at 20:10 UTC

    Use HTML::Template or other templating system:

    use strict; use warnings; use HTML::Template; my $headerStr = 'Perl variable names stored in mysql fields'; my $str =<<HTML; <HTML> <HEAD> <title><TMPL_VAR NAME=TITLE></title> ... some other header information, such as javascripts ... </HEAD> <BODY> HTML my $template = HTML::Template->new(scalarref => \$str); $template->param(TITLE => $headerStr); print "Content-Type: text/html\n\n", $template->output ();

    Prints:

    Content-Type: text/html <HTML> <HEAD> <title>Perl variable names stored in mysql fields</title> ... some other header information, such as javascripts ... </HEAD> <BODY>

    True laziness is hard work
Re: Perl variable names stored in mysql fields
by moritz (Cardinal) on Nov 12, 2009 at 19:54 UTC
Re: Perl variable names stored in mysql fields
by almut (Canon) on Nov 12, 2009 at 19:19 UTC
    $config{'pagename'} is printed exactly as that without interpretation...

    You might need an eval ... but it's hard to tell without knowing the context in which the snippet is being processed (which HTML templating system? (if any), etc.)

Re: Perl variable names stored in mysql fields
by keszler (Priest) on Nov 12, 2009 at 19:21 UTC
    use strict; my $header = '<html><head><title>$cfg</title></head></html>'; my $cfg = 'This is my title'; my $out; eval '$out = "'.$header.'"'; print $out;

    eval

    update - fixed typo/thinko

Re: Perl variable names stored in mysql fields
by dwm042 (Priest) on Nov 12, 2009 at 23:31 UTC
    You know, I inherited a web site and Perl code base that used a lots-of-code-in-the-db design (my predecessor had been fired for exceptionally irregular hours, and left behind no documentation) and I really disliked every bit of figuring it out and maintaining it. It's so much fun when out of 100 tables in a single MySql database, 95 contain data, some data changing by the second, and the remaining 5 contain everything from SQL queries to javascript to form letter code written in Perl.

    All it would have taken was one data accident that nailed the database instance, and the *code* to the application would have been lost.

      Hrm. I see the point. Store data, not code.

      I always thought that some applications like Wordpress (I know it's in PHP but the concept is similar) did something similar to this - but I realize it's more likely a combination of templates and DATA stored in the database.

      I'll rework. Thanks for the enlightenment, monks.

      -r-

Re: Perl variable names stored in mysql fields
by RyanJ (Initiate) on Nov 12, 2009 at 21:15 UTC

    Thanks for the replies so far, everyone...

    Templating is all fine and good, but I've got things stored in the database for other reasons as well. Is there NO way to get perl to evaluate the variables as they come out? (I'll give eval a shot...)

    -r-