in reply to Perl CGI w/ Javascript
Perl has many ways of letting you print more than one line at a time. Trying to print lines of code, be it JavaScript or HTML or whatever, one line at a time is not only tedious, it is extremely error prone. That is, you are bound to make mistakes.
Instead of trying to print lines of code, one line at a time, you could print one statement that spans more than one line:
Notice that i never used "\n". Now notice what happens when i want to print something that contains double quotes:print "<html> <body> Hello World </body> </html> ";
I have to escape the double quotes that are meant for the HTML renderer so the Perl interpreter does not close the quotation prematurely - but you already know this. You might have thought about using single quotes instead:print "<h1 align=\"right\">Hello World</h1>";
But you might have attempted to print out a newline "\n" and went back to double quotes and escaping. However, since Perl doesn't "freak out" on whitespace, you can do something like:print '<h1 align="right">Hello World</h1>';
But there are better ways. First off, single quotes are not a substitute for double quotes. They have different meaning. If you want to use the contents of a variable, you can use single quotes and concatenation:print '<html> <body> <h1 align="right">Hello World</h1> </body> </html> ';
But it is so much more convenient to use double quotes and interpolation:my $string = 'Hello World'; print '<h1 align="right">' . $string . '</h1>' . "\n";
If it weren't those darned escapes! Enter q (a substitute for the single quote) and qq (a substitute for the double quote - see Quote and Quote-like Operators for more).my $string = 'Hello World'; print "<h1 align=\"right\">$string</h1>\n";
If you look carefully you will notice that the parentheses are the delimiters for the string ... not double quotes. q and qq are magical in that they allow you to specify what the string delimiter(s) will be:print qq(<h1 align="right">$string</h1>\n);
Just about all of the "special" characters will do. But the topic is avoiding printing lines of code one line at a time, and doing so in a fashion that makes the code not only bug-free, but easy to maintain. Next stop, HERE docs.print qq(<h1 align="right">$string</h1>\n); print qq{<h1 align="right">$string</h1>\n}; print qq#<h1 align="right">$string</h1>\n#; print qq!<h1 align="right">$string</h1>\n!; # have to escape the delimiter: print qq/<h1 align="right">$string<\/h1>\n/;
HERE docs are a valid solution, but they really aren't my favorite. The concept is actually the same as the one for the q and qq operators - you specify the delimiters. However, instead of using some special character that is eventually going to show up in your data (and you are going to have to go right back to escaping again), you can choose a string ... a VERY_LONG_STRING_IF_NEED_BE.
This is probably a good solution for your level of programming right now. The solution i would like to recommend is HTML::Template, but i need to be sure that you have a good grasp of file permissions and opening files as a non-priveledged user.my $string = 'Hello World'; print <<HTML; <html> <body> <h1 align="right">$string</h1> </body> </html> HTML
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|