0

I have set of perl CGI webpages that I am attempting to do some refactoring on but have run into a wall.

Each of these scripts has some javascript and CSS currently written in the form:

my $JSCRIPT = << CODE;
function doStuff() {
  // Stuff
}
CODE;

my $CSS = <<BLOCK;
.css-stuff{}
BLOCK

The javascript and CSS is later loaded to the page using CGI

print $cgi->start_html( -script => [{-type => "javascript", -code => $JSCRIPT}],
                        -style => {-code => $CSS} );

Is there a good way to refactor this code so that instead of having copies of that $JSCRIPT in multiple files, I can have a single common.js or similar file? How do I include a file to accomplish this?

1
  • 1
    Worth noting that this usage of CGI is (soon to be) deprecated, and usage of a template engine such as Template Toolkit is recommended instead. See CGI::Alternatives. Commented Nov 4, 2014 at 17:21

2 Answers 2

1

Instead of -code use -src as shown in the CGI documentation.

print $q->start_html(-title=>'The Riddle of the Sphinx',
                     -script=>{-type=>'JAVASCRIPT',
                               -src=>'/javascript/sphinx.js'}
                     );

print $q->start_html(-title=>'The Riddle of the Sphinx',
                          -script=>[
                                    { -type => 'text/javascript',
                                      -src      => '/javascript/utilities10.js'
                                    },
                                    { -type => 'text/javascript',
                                      -src      => '/javascript/utilities11.js'
                                    },
                                    { -type => 'text/jscript',
                                      -src      => '/javascript/utilities12.js'
                                    },
                                    { -type => 'text/ecmascript',
                                      -src      => '/javascript/utilities219.js'
                                    }
                                 ]
                             );
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the following HTML to load a script from a file:

<script src="/path/to/file.js"></script>

Note that /path/to/file.js is relative to the document root of your webserver, and is not an absolute path on your filesystem.

As you may have noticed, embedding HTML/CSS/JavaScript in your Perl scripts is not very maintainable. I would recommend keeping your CSS and JavaScript in separate, static files and using a template library like Template Toolkit to generate your HTML. Using templates helps you separate your presentation logic from your business logic, and saves you from having to use CGI.pm's arcane methods of generating complex HTML.

Here's an example using Template Toolkit to populate the path to your JavaScript file at runtime:

foo.cgi

use strict;
use warnings;

use CGI;
use Template;

my $tt = Template->new or die Template->error;

my $q = CGI->new;
print $q->header;

my $js_file = '/path/to/file.js';
$tt->process('foo.tt', { js_file => $js_file }) or die $tt->error;

foo.tt

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello, Template Toolkit!</title>

    <script src="[% js_file %]"></script>
  </head>
  <body>
    <h1>Hello, Template Toolkit!</h1>
  </body>
</html>

Output

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello, Template Toolkit!</title>

    <script src="/path/to/file.js"></script>
  </head>
  <body>
    <h1>Hello, Template Toolkit!</h1>
  </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.