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

Community
  • 1
  • 1
JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • 2
    I think your solution set is the empty set. If you intend to associate perl with the file by a shbang then you need the first line. An SGML comment cannot occur before this line, so you're going to show the shbang--and most browsers will put it as it's own document--even if it makes non-standard HTML. – Axeman Mar 09 '10 at 05:22
  • Just add some Javascript to remove the shebang from the DOM! – jrockway Mar 09 '10 at 06:31
  • @Axeman, see empty set below ;) – JoelFan Mar 10 '10 at 03:06

5 Answers5

8

Read it and weep 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 ...

# -->
tripleee
  • 175,061
  • 34
  • 275
  • 318
JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • 2
    Neat trick. Although I would still advise against it. You should really have separate things in separate places. – Brad Gilbert Mar 10 '10 at 03:18
  • 4
    I commend you on your cleverness. – Axeman Mar 10 '10 at 04:03
  • 3
    +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 Mar 10 '10 at 09:31
5

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
  • 776,304
  • 153
  • 1,341
  • 1,358
5

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.

mob
  • 117,087
  • 18
  • 149
  • 283
3

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
  • 45,363
  • 14
  • 64
  • 102
0

Have you considered putting Perl inside of HTML?

Like ASP4 does?

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

JDrago
  • 2,079
  • 14
  • 15