Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: inserting HTML file in a PERL script

by wfsp (Abbot)
on May 29, 2005 at 11:43 UTC ( [id://461494] : note . print w/replies, xml ) Need Help??


in reply to Re^2: inserting HTML file in a PERL script
in thread inserting HTML file in a PERL script

To add to the advice from tchatzi.

The first time I came across Perl I was in a very similar situation to yourself. Perhaps the experiences I had maybe helpful.

Only make one small change at a time. You'll stand a better chance of having something that still works.

A point often made is that, as much as possible, keeping code and HTML separate makes maintaining both easier. A first step could be at least moving it all to the end of the script. Have a sub that returns the HTML.

my $name = 'John'; my $html = get_html(); # sub name corrected # later... sub get_html{ my $html = <<HTML; <html> <head> <title>my html</title> </head> <body> <p>Hi there $name</p> </body> </html> HTML return $html; }
The next step I took was to replace the variable with an HTML comment.
<p>Hi there <!-- name --></p>
and then the code would look something like
my $html = get_html(); $html =~ s/<!-- name -->/$name/;
The advantage here is that Frontpage can happily use the HTML.

After that it wasn't long before the HTML ended up in a file.

my $html; { local $/; my $file = 'template.html'; open my $fh, '<', $file or die "can't open $file to read: $!"; $data = <$fh>; close $fh or die "cannot close $file: $!"; }
This way you can maintain the HTML in FrontPage.

Once you get to this stage you start to realise you have trod a well worn path! Many have gone before you. As scooterm pointed out above there are many template modules available. I now use HTML::Template and find it excellent.

There are many other aspects of cgi scripts you may want to consider (there is a very good module for that too).

Hope this is of help. If after looking through the docs you still hit some snags (I hit plenty) come back, show what you were trying to do, what you have tried, what you expected. If you do that I have always found the monks very helpful.

Good luck!

update: Corrected name of sub in code snippet