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

Re: inserting HTML file in a PERL script

by dimar (Curate)
on May 29, 2005 at 09:26 UTC ( [id://461479] : note . print w/replies, xml ) Need Help??

in reply to inserting HTML file in a PERL script

Your question makes a little sense, but it is not exactly clear what is your underlying rationale and goals:

  • do you intend to serve the html over the web or just open them locally on your own machine?
  • do you intend to use msft FrontPage extensions in your pages or just using that because it is the only tool you have available?
  • are you using this variable interpolation method because you do not want to use any of the myriad templating solutions already out there?

Absent a better understanding of why you are going this specific path, please take a look at Template Toolkit. It seems as though you are trying to re-invent it, or something similar.


Replies are listed 'Best First'.
Re^2: inserting HTML file in a PERL script
by thetallblondguy (Initiate) on May 29, 2005 at 10:00 UTC

    Hello and thanks for your answer.

    - I will serve them on the web

    - I use FrontPage, because it is indeed the only tool that I have in order to design nice looking HTML pages.

    - I inherited of these scripts that I now have to manage myself, and dont feel like re-writing them all with new tools, as my knowledge of perl is very limited.

    These scripts have a myriad of embedded HTML codes with the print <<eof method, and I have to re-edit and make them look better.

    But for these .pl and .pm scripts, FrontPage is of little help for me, as the mixture of perl script and HTML code makes it almost impossible to properly edit.

      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

      What you can do is make your own page in FrontPage, as long as you feel more comfortable when working with it , instead of straight HTML code, and then go on the 'code' tab that Frontpage has and copy and paste the code or parts of it, between the print <<EOF;.......and......EOF
      Remember to add any variables that the preview code had.

      Or you can copy and paste the html code from the perl script to your code tab at frontpage(without any variables in it of course) and try to change it the way you want, and copy and paste it back from where you got it.

      This is realy not the right solution though, but if you don't feel comfortable with perl or html, then i think its your only way out.

      If there are more than one part of html code in the perl script, then remember to delete from the html code that fronpage will generate, the <html>,<body>,</html>,</body> tags, and also any other headers including <meta> tags, from the middle parts of your perl script.

      ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI