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

I have HTML files that I want to recreate multiple times with some different text. I already have the HTML code. What I want to do is run a perl script to read this code and save it to another file enclosed in print statements. How can I do this with putting the escapes in the proper places? For example,
"html text and tags" ==> print "html text and tags";
but some characters in the html code need the 'backslash' don't they?

Replies are listed 'Best First'.
Re: importing HTML code into perl
by growlf (Pilgrim) on Oct 27, 2001 at 05:10 UTC
    I have been heavily using HTML::Template lately for this purpose. It allows you to leave the HTML exactly as is, as filename.html but with an optional added ability to place dynamic HTML-like tags that are replaced with data from your application.

    If You are using this for CGI - the use is obvious, but I have also been using it as a static HTML file generator for dynamic content quite nicely.

    Check the terrific POD web reference at perldoc.com, http://www.perldoc.com/perl5.6.1/lib/HTML/Template.html for examples and documentation.

    I have recently even found that the DBI does infact work with the Template module seemlesly and easily - if you need to hook it to a database: I have many examples now of simple, to advanced useage.
Re: importing HTML code into perl
by Fletch (Bishop) on Oct 27, 2001 at 03:53 UTC

    A better solution would be to use something like HTML::Mason (see also masonhq.com) or Template (template-toolkit.org) to turn your HTML into templates rather than turning them into perl code. Also try a search for `template' or `templating' and that should turn up lots of useful information.

Re: importing HTML code into perl
by dws (Chancellor) on Oct 27, 2001 at 03:49 UTC
    I have HTML files that I want to recreate multiple times with some different text.

    Depending on how much customization you want to do, an alternative to using print statements is to include the HTML verbatim after __DATA__, slurp the HTML into a variable, then use regexp substitutions to tweak things. This looks something like:

    # slurp in the HTML my $html = do { local $/; <DATA> }; # customize the HTML here, via regexp $html =~ s/SUB1/$sub1/g; $html =~ s/SUB2/$sub2/g; ... etc. print $html; __DATA__ <html> ...
    I do this a lot for quick-and-dirty templating. It's a lot easier to tweak the HTML this way than to break it apart into separate print statements, and you don't have to worry about escaping special characters. For anything heavier-weight, look into one of the various template packages.

Re: importing HTML code into perl
by mandog (Curate) on Oct 27, 2001 at 06:59 UTC
Re: importing HTML code into perl
by tachyon (Chancellor) on Oct 27, 2001 at 16:44 UTC

    If you use a here page you do not need to quote stuff or add escapes, plus interpolation of variables is possible:

    my $stuff_to_interpolate = 'Just another Perl hacker,'; print <<END_OF_HTML; Content-type: text/html <html> <head> <title> $stuff_to_interpolate </title> </head> <body> Blah blah blah </body> </html> END_OF_HTML

    Make sure the token you use (in this case END_OF_HTML )appears on a line by itself and you will be fine

    cheerstachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print