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

Hey everyone, I have a question: How can I define variables with CGI.pm, such as I have some HTML that I have in a file. I want to define a variable of that HTML, such as the bgcolor of a specific table. How can I define a variable and read it into the HTML part? I know how to do that w/o CGI.pm, but with it, I can't figure it out, even after reading the pod docs on CGI.pm. Help! Thanks.

Replies are listed 'Best First'.
Re: Defining variables w/ CGI.pm/?/
by rob_au (Abbot) on Jul 21, 2001 at 11:05 UTC
    This question seems a little confused ... but if I am summarising what it is you are wanting to achieve correctly - You want to take HTML from a file and output that with variables which you have stored elsewhere - To achieve such a task, you don't need CGI.pm as much elegant solutions existing using HTML::Template and HTML::Mason.
     
    An example of how to achieve what (I think) you are wanting to achieve with HTML::Template follows:
     
    #!/usr/bin/perl use CGI; use HTML::Template; use strict; # Define the colour of the background variable, $bgcolor # my ($bgcolor) = "000000"; # Create CGI object # my ($cgi) = CGI->new(); # Create HTML::Template object, use template.html # my ($html) = HTML::Template->new( filename => 'template.html' ); # Substitute the value of $bgcolor into template.html # $html->param( 'bgcolor' => $bgcolor, ); # Print the output # print STDOUT $cgi->header(), $html->output; exit 0;

     
    ... and the HTML::Template file, template.html ...
     
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:/ +/www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>An example template file</title> </head> <body bgcolor="#<tmpl_var name="bgcolor">"> </body> </html>

     
    Now obviously, this is a very simple example of how you can interpolate variables into output HTML, but HTML::Template (and its contempararies) offer some very powerful ways to generate HTML from programming elements and thus help to separate these elements for later review and maintenance.
     
    The CGI module too has a role in such a system, but less so using its HTML output functions, given the handling of these from the template HTML files (which you seemed to infer to) and the HTML::Template module. The CGI module can still play a big role in a dynamic HTML/CGI application (not DHTML/CGI :) system as the method by which CGI passed parameters can be acted upon by your Perl script.
     
    For example, you could add in a line to the Perl script above, to have the background colour set via a CGI passed variable in the form of bgcolor=<value> in the query string, as follows:
     
    $bgcolor = ($cgi->param('bgcolor') =~ /^[\da-fA-F]{6}$/) ? $cgi->param +('bgcolor') : $bgcolor;

     
    This line inserted after the my ($cgi) = CGI->new(); would also allow the bgcolor variable in the template file to be set to a value passed by CGI.
     
    This is my poor attempt to answer your question ... but I hope I have addressed at least part of what you ask and given you something to play with and/or think about.
     

     
    Ooohhh, Rob no beer function well without!
Re: Defining variables w/ CGI.pm/?/
by tadman (Prior) on Jul 21, 2001 at 10:34 UTC
    Another candidate for "obfuscated questions".

    Look into Text::Template, which does the sort of thing you are talking about. CGI.pm takes input from the browser, not an HTML file. The Text::Template can, of course, take variables from CGI.pm where required.
Re: Defining variables w/ CGI.pm/?/
by bladx (Chaplain) on Jul 21, 2001 at 11:11 UTC
    Thank you for your help, it will be much appreciated, since I have been working on a big (for me at least,) project related to this subject! Thanks again, and sorry for phrasing this question badly, I asked about it in the CB, and got an answer earlier, but I posted to early ^^; sorry. Later. Andy