views:

182

answers:

5

In the spirit of the "Perl Preamble" where a script works properly whether executed by a shell script interpreter or the Perl interpreter...

I have a Perl script which contains an embedded HTML document (as a "heredoc"), i.e.:

#!/usr/bin/perl

... some Perl code ...

my $html = <<'END' ;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>

... more HTML ...

</HTML>
END

... Perl code that processes $html ...

I would like to be able to work on the HTML that's inside the Perl script and check it out using a web browser, and only run the script when the HTML is the way I want. To accomplish this, I need the file to be openable both as an HTML file and as a Perl script.

I have tried various tricks with Perl comments and HTML comments but can't get it quite perfect. The file as a whole doesn't have to be "strictly legal" HTML (although the embedded document should be)... just displayable in a browser with no (or minimal) Perl garbage visible.

EDIT: Solved! See my own answer

+6  A: 

This sounds like a path to pain. Consider storing the HTML in a separate file and reading it in within the script.

Ignacio Vazquez-Abrams
A: 

Have you considered putting Perl inside of HTML?

Like ASP4 does?

It's a lot easier that way - trust me ;-)

JDrago
Clearly Template::Refine is the best approach ;)
jrockway
Interesting... no ... **awesome**!
JDrago
+3  A: 

Maybe this is a job for Markup::Perl:

  # don't write this...
  print "Content-type: text/html;\n\n";
  print "<html>\n<body>\n";
  print "<p>\nYour \"lucky number\" is\n";
  print "<i>", int rand 10, "</i>\n</p>\n";
  print "</body>\n</html>\n";

  # write this instead...
  use Markup::Perl;
  <html><body><p>
  Your "lucky number" is
  <i><perl> print int rand 10 </perl></i>
  </p></body></html>

You could also drop the use Markup::Perl line and run your script like

perl -MMarkup::Perl my_page_with_embedded_perl.html

Then the page should render pretty well.

mobrule
That is definitely cool...
JoelFan
+3  A: 

Sounds to me like you want a templating solution, such as Template::Toolkit or HTML::Template. Embedding HTML in your code or embedding code in your HTML is a recipe for pain.

Dave Sherohman
+3  A: 

Read it and weap Mr. @Axeman... I now present to you the empty set:

</dev/fd/0 eval 'exec perl -x -S $0 ${1+"$@"}' #> <!--
#!perl

... some Perl code ...

my $html = << '<!-- END' ;  # -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>

... more HTML ...

</HTML>
<!-- END

... Perl code that processes $html ...

# -->
JoelFan
Neat trick. Although I would still advise against it. You should really have separate things in separate places.
Brad Gilbert
I commend you on your cleverness.
Axeman
+1 for awesome hackishness. But, for production code or any other circumstance where maintainability is even a minor concern, I still maintain that using templates (as suggested in the answer I submitted yesterday) will cause you far less pain in the long run.
Dave Sherohman