Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

I have a cgi that uses DBI. Occasionally the LD_LIBRARY_PATH env var is not set correctly and causes a auto-load error when it gets to the use DBI statement.

How can I set it in the cgi? Simply setting $ENV{LD_LIBRARY_PATH} does not work, even in a BEGIN block. The perlvar doc does say that %ENV changes affect child processes (not the parent implied?).

I saw one reference to a script that forks to a shell and execs itself, but that is not so useful when the shell itself has a problem. Also, this is a web process so there could be i/o problems.

Replies are listed 'Best First'.
Re: Environment Var Problem
by idsfa (Vicar) on May 09, 2006 at 17:57 UTC
    BEGIN { my $self = $0; # or hardcode it, depending upon need if ( $ENV{'LD_LIBRARY_PATH'} !~ m|/path/you/want| ) { $ENV{'LD_LIBRARY_PATH'} = "/path/you/want:$ENV{LD_LIBRARY_PATH}" +; exec $self, @ARGV; } } use DBI;

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
      That's interesting! How does it get past the exec statement? In other words, what prevents the process from getting stuck in a loop?

        It checks if the new path is in LD_LIBRARY_PATH before running itself again. Once the new path is there, it doesn't run itself anymore, breaking the loop.

Re: Environment Var Problem
by liverpole (Monsignor) on May 09, 2006 at 16:33 UTC
    No, the parent process will not have its ENV variable changed, since you're forking off a subprocess when you run a Perl script, and when control comes back to the parent, the original environment is restored.

    If I may suggest an alternative way ... use a statement like:

    use lib [library path];
    where [library path] is the path of the modules you want to include.

    That way you have direct control, from within the script, of where you get the module(s).


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Good try, but it doesn't work. Neither does a -Idir switch.