Re: Inserting JavaScript into Perl Script
by tadman (Prior) on Nov 18, 2002 at 02:14 UTC
|
A quick idea of what you can do:
print <<END;
// JavaScript goes here
// ...in large quantities.
END
# Perl continues here
The last part can be changed to 'END' if you want to prevent variables such as $foo from being interpolated. Of course, you might want this, so the option is there.
Another way to do this is using standard quotation:
print qq~
// JavaScript goes here
// ...as long as there's no tildes (which end it)
~;
# Perl continues here
As a tip, qq interpolates like double quotes,
while q alone is like a single quoted string. Carefully choose your delimiter, though, since it can confuse the parser if it appears within your JavaScript. Tilde is a fairly safe bet, I would presume. | [reply] [d/l] [select] |
Re: Inserting javascript into perl script
by bart (Canon) on Nov 18, 2002 at 00:44 UTC
|
To perl, it makes no difference whether it's emitting html or Javascript. So yes, any way to print the strings to STDOUT is OK. Here-docs are nice (see perldata). They allow you to easily embed multiline literal strings in you scripts. But actually, simple quoted strings can be multiline, too.
| [reply] |
Re: Inserting javascript into perl script
by Louis_Wu (Chaplain) on Nov 18, 2002 at 00:41 UTC
|
Are you asking how to use CGI to transmit a webpage which contains Javascript? If so, use CGI as you normally would for your page, and have the JS embedded in the HTML you send.
That seems too obvious, I must be missing something. Could you give some more details and some code you've tried? | [reply] |
|
It's not an html page, it's a .pl script, with html code near the end. Was just wondering if javascript needs print statements in front of it below the http header.. i'm assuming i don't need print statements if it's placed above the http header. -Lisa
| [reply] |
|
Your absoltely right. The client web browser will utilize ESP::ThoughtTransfer to connect to the web server, download the script, look through it, find all the javascript code and magically execute it.
| [reply] |
Re: Inserting javascript into perl script
by mikeirw (Pilgrim) on Nov 18, 2002 at 04:29 UTC
|
$query = new CGI;
print $query->header;
$JSCRIPT=<<END;
// Ask a silly question
function riddle_me_this() {
var r = prompt("What walks on four legs in the morning, " +
"two legs in the afternoon, " +
"and three legs in the evening?");
response(r);
}
// Get a silly answer
function response(answer) {
if (answer == "man")
alert("Right you are!");
else
alert("Wrong! Guess again.");
}
END
print $query->start_html(-title=>'The Riddle of the Sphinx',
-script=>$JSCRIPT);
...which I lifted straight from perldoc CGI. | [reply] [d/l] [select] |
|
Another way to approach it if you're using CGI.pm is like this...
SNIP
...which I lifted straight from perldoc CGI.
And little way down is the trick for when you have a lot of js which doesn't change between page displays, that is storing the js in a separate file and including it in a <SCRIPT SRC=...> tag.
print $q->start_html(-title=>'The Riddle of the Sphinx',
-script=>{-language=>'JAVASCRIPT',
-src=>'/javascript/sphinx.js'}
);
This is a GOOD thing for a number of reasons including the fact that it forces you to separate client and server which can be good for debugging weird stuff.
Aristotle posted an excellent comment 213336 about the problems of combined js/perl which you should bear in mind. This doesn't menan don't do it but you do need to withe rhave full control of your clients or a way to degrade gracefully if they don't support javascript.
Dingus Enter any 47-digit prime number to continue. | [reply] [d/l] |
Re: Inserting javascript into perl script
by UnderMine (Friar) on Nov 18, 2002 at 10:17 UTC
|
| [reply] |