The immediate question:
Is there a way I can set the library path depending on %ENV (specifically some HTTP/HTML $ENV's) in time to control the loading of Perl modules?
Background:
I have a CGI script, and a whole slew of locally written Perl modules. The script contains a suitable
use lib '/home/sources/bin/perllib';
before it attempts to load any of the local modules.
I have a development server, with several development trees, each of which contains a copy of the script, and some of them contain copies of the modules.
Up to now, if I want the script on the development server to use development versions of the libraries, I edit the text to contain
use lib '/export/home/etgdev-2/perllib';
instead.
(Note that the location of the modules on the live server is not in the same tree, so I can't use a relative path or FindBin).
What I would like to do, is to make this decision automatic, so that I wouldn't have to edit the file. I first tried
That doesn't work, because use lib runs at compile time.
So my next attempt was
(I took the list of variables to check from CGI.pm)
This still doesn't work, in two respects:
First it still loads the main modules, and secondly, it gives a Perl warning in the log file about an unitialised value in concatenation on the line with the $1 above.
If I put a print STDERR in before the use lib line, it writes what I would expect to the log file - but it appears after the warning.
It seems to me, therefore, that even though I've put the code in a BEGIN block, there are still two stages:
- first, it's compiling the use lib line, and complaining that $1 is unset - presumably because it hasn't yet set up the %ENV hash.
- then it's running the statements in the BEGIN block, and by this time it has set up %ENV, so the output is as expected.
Does anybody know if
1) My analysis is correct?
2) This is a bug or a feature?
3) If there is a way round (or a better way to achieve what I'm trying to do)?
I'm using Gnu perl 5.6.1, Apache 1.3.22 on Fedora Core 3.
Is there a way I can set the library path depending on %ENV (specifically some HTTP/HTML $ENV's) in time to control the loading of Perl modules?
Background:
I have a CGI script, and a whole slew of locally written Perl modules. The script contains a suitable
use lib '/home/sources/bin/perllib';
before it attempts to load any of the local modules.
I have a development server, with several development trees, each of which contains a copy of the script, and some of them contain copies of the modules.
Up to now, if I want the script on the development server to use development versions of the libraries, I edit the text to contain
use lib '/export/home/etgdev-2/perllib';
instead.
(Note that the location of the modules on the live server is not in the same tree, so I can't use a relative path or FindBin).
What I would like to do, is to make this decision automatic, so that I wouldn't have to edit the file. I first tried
Code:
if (virtual_host() =~ [i]a suitable regexp[/i]) {
use lib "/export/home/$1/perllib";
} else {
use lib "/home/sources/bin/perllib";
}
That doesn't work, because use lib runs at compile time.
So my next attempt was
Code:
BEGIN {
my $vh = $ENV{HTTP_X_FORWARDED_HOST} ||
$ENV{HTTP_HOST} ||
$ENV{SERVER_NAME};
if ($vh =~ [i]the same regexp[/i]) {
use lib "/export/home/$1/perllib";
} else {
use lib "/home/sources/bin/perllib";
}
}
This still doesn't work, in two respects:
First it still loads the main modules, and secondly, it gives a Perl warning in the log file about an unitialised value in concatenation on the line with the $1 above.
If I put a print STDERR in before the use lib line, it writes what I would expect to the log file - but it appears after the warning.
It seems to me, therefore, that even though I've put the code in a BEGIN block, there are still two stages:
- first, it's compiling the use lib line, and complaining that $1 is unset - presumably because it hasn't yet set up the %ENV hash.
- then it's running the statements in the BEGIN block, and by this time it has set up %ENV, so the output is as expected.
Does anybody know if
1) My analysis is correct?
2) This is a bug or a feature?
3) If there is a way round (or a better way to achieve what I'm trying to do)?
I'm using Gnu perl 5.6.1, Apache 1.3.22 on Fedora Core 3.