make my file readable as either Perl or HTML

时光总嘲笑我的痴心妄想 提交于 2019-12-05 10:14:02
JoelFan

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 ;-)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!