我想使用
HTML :: Template模块.但是,它并没有安装在我用来开发CGI脚本的服务器上.
可以在运行时加载模块:我在本地Perl安装中找到Template.pm文件,并将文件上传到我正在使用的服务器.
#!/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;
解决方法
这是我做的:
cgi-bin/script.pl cgi-bin/lib/HTML/Template.pm
在script.pl中(除非你在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
如果HTML::Template是真正可选的,请阅读perldoc -f require.
参见perlfaq8 How do I keep my own module/library directory?和What’s the difference between require and use?.