views:

373

answers:

4

Good Day,

I would like to use the HTML::Template module. However, it is not installed on the server I'm using to develop CGI scripts.

Is it possible to load a module at runtime: I found the Template.pm file on my local Perl installation and uploaded the file to the server I'm using.

#!/usr/bin/perl -w

use CGI qw(:standard :html4);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

# use HTML::Template;

use Template;

# my $package = "HTML::Template";
# eval {
# (my $pkg = $package) =~ s|::|/|g; # require need a path
# require "$pkg.pm";
# import $package;
# };
# die $@ if( $@ );

# open the HTML template
my $template = HTML::Template->new(filename => 'test.tmpl');

# fill in some parameters in the template
$template->param(home => $ENV{HOME});
$template->param(path => $ENV{PATH});

# send the obligatory Content-Type
print "Content-Type: text/html\n\n";

# print the template
print $template->output;
A: 

Yes it is. Look at Module::Runtime. I would install your HTML module though, even in a local install directory. It's probably less hassle.

Mark Lewis
Do you mean FTP the Template.pm to the local install directory on the server?
coson
+9  A: 

Here is what I do:

     cgi-bin/script.pl
     cgi-bin/lib/HTML/Template.pm

In script.pl (unless you are running under mod_perl):

 use FindBin qw( $Bin );
 use File::Spec::Functions qw( catfile );
 use lib catfile $Bin, 'lib';
 use HTML::Template;

 # The rest of your script

If HTML::Template is truly optional, read perldoc -f require.

See also How do I keep my own module/library directory? and What's the difference between require and use? in perlfaq8.

Sinan Ünür
Yeah, this looks like what you actually want to do, just add another place for modules that perl knows to look for, which is what the `use lib ...` line is doing (the two lines above it are about finding the modules location relative to the script)
Cebjyre
+2  A: 

HTML::Template and Template are different Perl modules. If you want to use HTML::Template, you will need to use HTML::Template; at the top of the script to import that package.

Ensure that you copied the HTML/Template.pm file from your local machine to the server, rather than Template.pm.

rjh
The problem is I don't know which is the correct HTML::Template module. When I search under my Windows installation, I see four HTML.pm and a Template.pm. I'm not sure which one I'm supposed to use. I'm sure that I could try this out one file at a time.
coson
It will be called Template.pm but will be inside a directory called HTML. This is how Perl modules are laid out, e.g. Moose::Meta::Class is in Moose/Meta/Class.pm
rjh
yeah, I finally figured that out based on the previous answer.
coson
+6  A: 

This is similar to Sinan's answer, but uses local::lib:

Set up your files as:

cgi-bin/script.pl
cgi-bin/lib/HTML/Template.pm

And in your script:

use strict;
use warnings;
use local::lib 'lib';
use HTML::Parser;

The advantage of local::lib is you can install modules from CPAN directly into a directory of your choosing:

perl -MCPAN -Mlocal::lib=lib -e 'CPAN::install("HTML::Parser")'
Ether