Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
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

In reply to Re^3: inserting HTML file in a PERL script by wfsp
in thread inserting HTML file in a PERL script by thetallblondguy

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.