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

Is it possible to set environment variables for Oracle inside perl for use with Class::DBI::Oracle? I'm getting an error from DynaLoader saying it cant find a library. My code works fine if I set LD_LIBRARY_PATH in my shell and then call the perl program, but I want to know if I can do it inside the perl. I've tried stuff along the lines of:

BEGIN { $ENV{LD_LIBRARY_PATH} .= ":/users/oracle/product/9.2.0/lib"; $ENV{ORACLE_HOME} ||= "/users/oracle/product/9.2.0"; } package MyDatabase::DBI; use strict; use base 'Class::DBI::Oracle'; # connect to the database. MyDatabase::DBI->connection('dbi:Oracle:fred', 'wilma', 'barney'); # more code to follow etc...

But this does not seem to work. Any ideas?

Thanks, mildside.

Replies are listed 'Best First'.
Re: Class::DBI::Oracle environment variable issue
by herveus (Prior) on Nov 22, 2005 at 14:51 UTC
    Howdy!

    Given your findings, what you need to do in the BEGIN block is something along the following lines:

    BEGIN { if ($ENV{LD_LIBRARY_PATH} doesn't contain the necessary thingy and $ENV{ORACLE_HOME} doesn't contain the necessary thingy) { set $ENV as above exec $0, @_; } }

    This re-executes the script, but the environment has been changed. Double check the syntax for all the bits; the concept of "diddle %ENV and exec myself" will do the trick. I know I had to do stuff like that with Sybase.

    yours,
    Michael
      Thanks herveus, this has worked a treat! I changed the 'and' to an 'or', and slightly changed the 'exec', but otherwise followed your example pretty closely. My new BEGIN block follows:
      BEGIN { if (!defined($ENV{'ORACLE_HOME'}) || !defined($ENV{'LD_LIBRARY_PA +TH'})) { $ENV{'ORACLE_HOME'} = "/users/oracle/product/9.2.0"; $ENV{'LD_LIBRARY_PATH'} = $ENV{'ORACLE_HOME'} . "/lib"; exec($^X, $0, @ARGV); } }
      Thanks again,
      mildside
Re: Class::DBI::Oracle environment variable issue
by VSarkiss (Monsignor) on Nov 22, 2005 at 15:36 UTC
      Thanks VSarkiss. I had kind of realised that this was the problem, but had thought maybe there was a way to tell DynaLoader where to look for the library. I even checked the doco and found an array that DynaLoader uses for this purpose, but couldn't get it to do what I want. I suspect that even in this case it is, as you say, too late.

      In any case, herveus's solution works and I'm happy.

Re: Class::DBI::Oracle environment variable issue
by ptum (Priest) on Nov 22, 2005 at 05:05 UTC

    Are you trying to access environment variables from within a CGI script? Are you using Apache? If "yes" to both of those questions, you need to explicitly pass the environment variables through Apache as described here: http://httpd.apache.org/docs/1.3/env.html.

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      Thanks for your reply ptum. However, no, this is not a CGI I script, and Apache is not involved.