Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Inserting javascript into perl script

by wolverina (Beadle)
on Nov 18, 2002 at 00:35 UTC ( [id://213629]=perlquestion: print w/replies, xml ) Need Help??

wolverina has asked for the wisdom of the Perl Monks concerning the following question:

Anyone have a simple example showing how to insert a javascript into a cgi perl script? For instance, do you have to put a print statement in front of each javascript line? -Lisa

Replies are listed 'Best First'.
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.
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.
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?

      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
        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.
Re: Inserting javascript into perl script
by mikeirw (Pilgrim) on Nov 18, 2002 at 04:29 UTC

    Another way to approach it if you're using CGI.pm is like this...

    $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.
      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.
Re: Inserting javascript into perl script
by UnderMine (Friar) on Nov 18, 2002 at 10:17 UTC
    From what you are saying I would suggest looking at something like HTML::Template. This would allow you to seperate out the HTML/Javascript from the perl side. This is especially useful when you are receiving constantly updated html from 3rd parties.

    Hope this helps
    UnderMine

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://213629]
Approved by tadman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-06-14 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.