generating html from perl

N

Nick Wedd

I have an html page, which I want to generate from a cgi script. I have
tried to do this by putting the entire html into a script like this:


#!/usr/bin/perl -w

print <<Whole_file;

<html>
<head>
.... all the rest of the html ....
</body>
</html>
Whole_file


This almost works. But my html includes some javascript, which includes
some escape sequences, some of which get expanded by this process. It
expands each "\n" in my javascript into "
", which I can live with. But it also expands
pattern = /\[([^)]+)/;
to
pattern = /[([^)]+)/;
although it does not expand
pattern = /\[([^(]*)\(([^)]*)\)([^]]*)/;


Is there a recommended way of getting round this? Ideally, I would like
it to leave all the html as it is.

Nick
 
G

Gunnar Hjalmarsson

Nick said:
I have an html page, which I want to generate from a cgi script. I
have tried to do this by putting the entire html into a script like
this:

#!/usr/bin/perl -w

print <<Whole_file;

<html>
<head>
.... all the rest of the html ....
</body>
</html>
Whole_file

This almost works. But my html includes some javascript, which
includes some escape sequences, some of which get expanded by this
process. It expands each "\n" in my javascript into "
", which I can live with. But it also expands
pattern = /\[([^)]+)/;
to
pattern = /[([^)]+)/;
although it does not expand
pattern = /\[([^(]*)\(([^)]*)\)([^]]*)/;

Is there a recommended way of getting round this? Ideally, I would
like it to leave all the html as it is.

If you want to use the here-document syntax, you can do:

print <<'Whole_file';
------------^----------^

See http://www.perldoc.com/perl5.8.0/pod/perlop.html#<<EOF

Another approach is to keep the HTML page in a separate file, open and
read it into a variable, and print that variable.
 
N

Nick Wedd

Gunnar said:
Nick said:
I have an html page, which I want to generate from a cgi script. I
have tried to do this by putting the entire html into a script like
this:
#!/usr/bin/perl -w
print <<Whole_file;
<html>
<head>
.... all the rest of the html ....
</body>
</html>
Whole_file
This almost works. But my html includes some javascript, which
includes some escape sequences, some of which get expanded by this
process. It expands each "\n" in my javascript into "
", which I can live with. But it also expands
pattern = /\[([^)]+)/;
to
pattern = /[([^)]+)/;
although it does not expand
pattern = /\[([^(]*)\(([^)]*)\)([^]]*)/;
Is there a recommended way of getting round this? Ideally, I would
like it to leave all the html as it is.

If you want to use the here-document syntax, you can do:

print <<'Whole_file';
------------^----------^

See http://www.perldoc.com/perl5.8.0/pod/perlop.html#<<EOF

Thank you. I now have the script working.
Another approach is to keep the HTML page in a separate file, open and
read it into a variable, and print that variable.

Nick
 
L

Louis Erickson

: In message <[email protected]>, Gunnar
:>Nick Wedd wrote:
<snip>
:>> Is there a recommended way of getting round this? Ideally, I would
:>> like it to leave all the html as it is.
:>
:>If you want to use the here-document syntax, you can do:
:>
:> print <<'Whole_file';
:>------------^----------^
:>
:>See http://www.perldoc.com/perl5.8.0/pod/perlop.html#<<EOF

: Thank you. I now have the script working.

:>Another approach is to keep the HTML page in a separate file, open and
:>read it into a variable, and print that variable.

If you've only one, you can use the DATA filehandle and __DATA__ keyword.

If you have a line in your script that reads only "__DATA__", anything after
that will be available via the DATA file handle.

(I don't know when this was introduced; it may not be true in every version
of Perl. Works in 5.6.0, and I suspect later.)

So, you can say:

# Bunch of Perl code...

print while(<DATA>);

# Bunch more perl code

__DATA__
<html>
<!-- Bunch of unedited HTML/javascript/whatever -->
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top