in reply to Re^2: Add Javascript file and Javascript code using CGI.pm
in thread Add Javascript file and Javascript code using CGI.pm

You'd better remember me this Kwanzaa.

Take one; live, nude CGI

use strict; use warnings; use CGI qw( header start_html end_html h1 start_script end_script ); print header(), start_html(-title => "I swear to RTFM in the future", -head => [ start_script({ -type=>'text/javascript', -src=>'/main1.js' }), end_script(), start_script({ -type=>'text/javascript', -src=>'/main2.js' }), end_script(), ] ), h1("Oh, hai! I can haz script?"); print <<"SomeJS"; <script type="text/javascript"><!--//--><![CDATA[//><!-- alert("Oh, noes!"); //--><!]]> </script> SomeJS print end_html();

Take 2; with Template/__DATA__

use strict; use warnings; use Template; use CGI qw( header ); my @scripts = qw( main1.js main2.js ); print header(); my $tt2 = Template->new; $tt2->process(\*DATA, { title => "", script_includes => \@scripts, script_body => "alert('Oh, noes!');", }) or warn $tt2->error; exit 0; __DATA__ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>[% title || "I swear to RTFM in the future" %]</title> [%- FOR script IN script_includes %] <script type="text/javascript" src="/[% script %]"></script> [%- END %] </head> <body> <h1>Oh, hai! I can haz script?</h1> <script type="text/javascript"><!--//--><![CDATA[//><!-- [% script_body %] //--><!]]> </script> </body> </html>

To reiterate. The right way to do this is with separation of concerns; template apart from data apart from controller. I don't advocate doing either one of those except for throwaways, personal scripts, or to simplify installation for a CGI that is guaranteed never to grow.