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

Is there anyway to turn the __DATA__ section into a template that can be used more effectively when outputting things like HTML? For example:
my $name = "Mr. Smith"; print while <DATA>; __DATA__ Content-type: text/html <HTML><BODY>Hello $name!</BODY></HTML>
I am looking at easy ways to templatize a web site where the templates are unique to the scripts (such as a login page) and rarely change... and when they do change, often changes come with the CGI script as well... Thanks!

Replies are listed 'Best First'.
(jeffa) Re: Getting data *INTO* __DATA_
by jeffa (Bishop) on Jul 09, 2002 at 02:55 UTC
    This can be accomplished with a templating module, such as HTML::Template -
    use strict; use HTML::Template; my $name = 'Mr. Anderson'; my $data = do {local $/; <DATA>}; my $tmpl = HTML::Template->new( scalarref => \$data, ); $tmpl->param( name => $name, ); print $tmpl->output; __DATA__ Content-type: text/html <html><body>Hello <tmpl_var name>!</body></html>
    I would use CGI qw(header); however, instead of hand-coding the header in __DATA__.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Getting data *INTO* __DATA_
by atcroft (Abbot) on Jul 09, 2002 at 03:00 UTC

    While not quite what you were asking, you might want to know that HTML::Template (also mentioned in a tutorial entitled HTML::Template Tutorial) does not require a file to contain the template, but can also be called initially with a reference to an array containing the template, which might be easier to manipulate. As well, HTML::Template can be used to generate text other than HTML.

    As to __DATA__, I thought it was read-only (unless you start manipulating the source for later)... I could be wrong, however, and hope other monks will speak so we may both learn (and correct any errors I may have made, for which I ask forgiveness beforetimes)....

    Update: Brother jeffa responded before I could submit, and in much better style. A very good read, and my hood's off to 'im.....

    Update: Yes, Brother jeffa, I have indeed enjoyed the feel of braincells turning to mush upon exposure to the module you mentioned, and thus why I thought I might be incorrect....

      Have you seen Conway and Ingerson's evil Inline::Files yet? This is way cool:
      use strict; use vars qw($DATA); use Inline::Files; open DATA or die $!; chomp ($_ = <DATA>); close DATA or die $!; print "you have run this script @{[++$_]} times\n"; open DATA, ">$DATA" or die $!; print DATA $_; close DATA or die $!; __DATA__ 0

      jeffa

      you have run this script 1 times? grammar shmammar ;)
        Hmmm... You guys really got my wheels turning on this one :-) So, now, I am invisioning the use of both HTML::Template and Inline::Files. For example, in a login script using cookies as the method or remembering a previous login, I can see a template section for __LOGGEDIN__ and __LOGGEDOUT__ with totally different HTML content. Anyways, thanks for the tips, they have been quite helpful. I will post an update when I get the code written (which will be tomorow, as it is now bed time for me!)
Re: Getting data *INTO* __DATA_
by TexasTess (Beadle) on Jul 10, 2002 at 02:33 UTC
    I wrote an html template today, saved a generic template and placed #WORDS where I knew values would change. I read the file in to an array one line at a time and then, because i knew what lines the #'s were on, I s/#THIS/$form{'that'); on each line that needed s/ing then printed the array out to a dynamically created unique output file...worked rather nifty if I don't say so myself! I guess though, I am missing your point? What is usually before the header print? please clarify for this old lady?

    TexasTess
    "Great Spirits Often Encounter Violent Opposition From Mediocre Minds" --Albert Einstein