Zoran Simic
Zoran Simic

Reputation: 10333

How to reuse the functions defined in one script from another in Perl, running as cgi under Apache?

I'm trying to organize a few perl scripts in a way so I can reuse some functions between them. The directive 'require' however doesn't seem to work when the script is ran as CGI under Apache. I've tried this:

require "common.pl"

but it doesn't work under Apache.

I've also tried this:

use File::Basename;
use Cwd qw(abs_path);
require abs_path(dirname($0))."/common.pl";

which works both on the command line and under Apache on my local server but not on my webhost's server.

Do you know what the proper way to 'require' a perl script is so it works on both the command line and under Apache?

Edit: I'm looking for a way (not necessarily using 'require') to make the functions defined in 'common.pl' available to the script calling this here. So basically I have 2 scripts 'foo.pl' and 'bar.pl' from which I'd like to reuse the functions written in 'common.pl'. What is the right way to do that in Perl? 'require' works fine on the command line, but not under Apache... I have no control over %INC, and I can't hardcode the full path to 'common.pl'.

I can't hardcode the path to 'common.pl' because this set of scripts has to run on 3 different servers, each having a different absolute path for it.

Edit2: The error message I get happens when running using mod_perl (I was mistakenly stating this was all happening when running under Apache/cgi, but I had mod_perl on on one of the 3 setups, the other 2 setups were running scripts as regular cgi scripts, and the error I was seeing was unrelated to the 'require' statement, which works fine under regular cgi). The error is as follows:

Can't locate common.pl in @INC (@INC contains: /Library/Perl/Updates/5.10.0/darwin-thread-multi-2level /Library/Perl/Updates/5.10.0 /System/Library/Perl/5.10.0/darwin-thread-multi-2level /System/Library/Perl/5.10.0 /Library/Perl/5.10.0/darwin-thread-multi-2level /Library/Perl/5.10.0 /Network/Library/Perl/5.10.0/darwin-thread-multi-2level /Network/Library/Perl/5.10.0 /Network/Library/Perl /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level /System/Library/Perl/Extras/5.10.0 . /usr) at /.../check.pl

Upvotes: 1

Views: 354

Answers (2)

Sherm Pendley
Sherm Pendley

Reputation: 13612

Why would hardcoding three different paths be a problem?

use Sys::Hostname;
my $host = hostname();
$host eq 'foo' and push @INC, '/foo';
$host eq 'bar' and push @INC, '/bar';
$host eq 'baz' and push @INC, '/baz';
require 'common.pl';

Granted, it's not the most elegant solution, but if it works...

Upvotes: 1

TardisX
TardisX

Reputation: 697

Configuring software to suit different deployments is a fact of life. Sometimes 'magic' can help, but often it can lead to inpenetrable problems as well.

Do you have control over the environment? I would set environment variables (http://httpd.apache.org/docs/current/mod/mod_env.html) in your Apache config and use them as the path in your require.

Upvotes: 2

Related Questions