开发者

make my file readable as either Perl or HTML

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 Transit开发者_如何转开发ional//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


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 ...

# -->


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


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.


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.


Have you considered putting Perl inside of HTML?

Like ASP4 does?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜