使我的文件可读为Perl或HTML [英] make my file readable as either Perl or HTML

查看:92
本文介绍了使我的文件可读为Perl或HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本着 Perl前言,其中脚本可以正常工作,无论是由shell脚本解释器还是Perl解释器执行...

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

我有一个Perl脚本,其中包含一个嵌入式HTML文档作为heredoc),即:

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


$ b b

我想能够处理Perl脚本中的HTML,并使用Web浏览器检查它,并且只有在我需要的时候才运行脚本。要实现这一点,我需要该文件作为一个HTML文件和作为Perl脚本可打开。

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.

我尝试过各种技巧与Perl评论和HTML评论,但可以不会让它相当完美。文件作为一个整体不一定是严格合法的HTML(虽然嵌入的文档应该)...只是可以在浏览器中显示没有(或最少的)Perl垃圾可见。

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.

编辑:解决了!看我自己的答案

Solved! See my own answer

推荐答案

阅读它和武器@Axeman ...我现在向你< ::

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

# -->

这篇关于使我的文件可读为Perl或HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆