1

I have to add /oracle/v10.2.0/lib to LD_LIBRARY_PATH to use DBI module to connect to Oracle.

If I set LD_LIBRARY_PATH in shell before executing perl script, everything is ok.

But it does not work from script:

BEGIN {
  $ENV{'LD_LIBRARY_PATH'}='/oracle/v10.2.0/lib';
}
use DBI;

When I execute the script I get the error:

install_driver(Oracle) failed: Can't load '/usr/apps/perl5/site_perl/5.8.8/i686-linux/auto/DBD/Oracle/Oracle.so' for module DBD::Oracle: libclntsh.so.10.1: cannot open shared object file: No such file or directory at /usr/apps/perl5/5.8.8/i686-linux/DynaLoader.pm line 230. at (eval 3) line 3 Compilation failed in require at (eval 3) line 3. Perhaps a required shared library or dll isn't installed where expected at /var/tmp/getTraceDB.pl line 23

Gregory Danenberg
  • 519
  • 2
  • 9
  • 15

2 Answers2

5
BEGIN {
$ENV{'LD_LIBRARY_PATH'}.='/oracle/v10.2.0/lib';
exec($^X, $0, @ARGV);
}
use DBI;

Refer Runtime Linker and LD_LIBRARY_PATH for proper description.

  • 1
    Exec is the right way, but I think you'd get yourself into an endless loop there. You should only exec when the right path wasn't already in `$ENV{LD_LIBRARY_PATH}` because there is no tail condition. – Axeman Mar 16 '12 at 12:23
  • with exec it's working, thanks. The only problem is that perl debugger ("-d") does not work anymore...exec executes script straight away – Gregory Danenberg Mar 16 '12 at 13:48
0

Far better would be to have LD_LIBRARY_PATH set in your .profile or similar. It has to be visible at the time the ELF loader starts up, which is waaay before even /usr/bin/perl is running, let alone the BEGIN block of your script. The exec() trick works by restarting the entire binary if it had to add the path, but better would be for it to always exist in your environment always.

LeoNerd
  • 8,344
  • 1
  • 29
  • 36