Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 218
JoelFan Avatar asked Mar 09 '10 04:03

JoelFan


1 Answers

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

# -->
like image 89
JoelFan Avatar answered Sep 22 '22 02:09

JoelFan