1

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
10
  • 1
    require dies when it fails, so what's the error? Commented Mar 28, 2011 at 4:15
  • Doesn't it have more to do with you PERL5LIB path? Commented Mar 28, 2011 at 4:18
  • The issue is I want to include somehow (not necessarily with 'require') the functions that are in 'common.pl'... and I can't find a way where it works on my web host's configuration Commented Mar 28, 2011 at 4:25
  • Make sure your path to common.pl is correct. Especially checking for symbolic links - you may need to turn on followsymlinks, too. Commented Mar 28, 2011 at 4:35
  • No symlinks are involved. All the scripts are in the same folder (no symlinks, actual files are there). Everything works fine on the command line, but not when running under cgi/apache for some reason. I was expecting this to work just fine (vanilla case...) that's what's puzzling me. Can't see what I'm doing wrong. Commented Mar 28, 2011 at 4:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't have control over environment variables, and it's desirable that the scripts be as "self contained" as possible (no other setup than placing the scripts in the right place)
1

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...

1 Comment

This will indeed work for me, not very elegant but acceptable. Thanks.

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.