phileas fogg
phileas fogg

Reputation: 1933

Perl: why aren't my environment variables being set?

I am trying to connect to an Oracle DB using perl-DBD-Oracle. I need to have the following environmental variables set:

export LD_LIBRARY_PATH=/home/x/lib/ora10gclient
export TNS_ADMIN=/home/x/lib/ora10gclient
export PATH=/home/x/lib/ora10gclient:/usr/kerberos/bin:/home/x/bin:/home/x/bin64:/usr/local/sbin:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin
export ORACLE_HOME=/home/x/lib/ora10gclient

This script is run by a headless user and I have placed those lines in the .bash_profile file for that headless user.

The problem is, the headless user ssh's into the machine where this script is run and the bash profile isn't sourced. So I thought I would just set those environment variables in the BEGIN block of the script like so:

    BEGIN {
  $ENV{'LD_LIBRARY_PATH'} = '/home/x/lib/ora10gclient';
  $ENV{'TNS_ADMIN'} = '/home/x/lib/ora10gclient';
  $ENV{'PATH'} = '/home/x/lib/ora10gclient:/usr/kerberos/bin:/home/x/bin:/home/x/bin64:/usr/local/sbin:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin';
  $ENV{'ORACLE_HOME'} = '/home/x/lib/ora10gclient';
}

However, the script fails claiming it can't find Oracle.pm, something that it doesn't do when those environment variables are set.

I've sprinkled print statements throughout the script to confirm that it does indeed set the environment variables. And even printed the environment variables out the line before attempting to create a handle to the DB (which is where the script fails) and confirmed that they are still set to the correct values.

I'm not spawning any child processes either. Below is a simplified version of my script which fails in the same manner.

    #!/usr/local/bin/perl

use warnings;
use strict;
BEGIN {
  $ENV{'LD_LIBRARY_PATH'} = '/home/x/lib/ora10gclient';
  $ENV{'TNS_ADMIN'} = '/home/x/lib/ora10gclient';
  $ENV{'PATH'} = '/home/x/lib/ora10gclient:/usr/kerberos/bin:/home/x/bin:/home/x/bin64:/usr/local/sbin:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin';
  $ENV{'ORACLE_HOME'} = '/home/x/lib/ora10gclient';
}

use DBI;


testConnect();


sub testConnect {
  my $orclPort = 1521;
  my $orclHost = '<oraclehost>';
  my $orclUser = '<user>';
  my $srvName = '<servicename>';

  my $db = "DBI:Oracle:service_name=$srvName;host=$orclHost;port=$orclPort";
  my $orclDBHndl = DBI->connect($db, $orclUser, $orclUser) or die "could not connect: $DBI::errstr\n";
}

Any ideas on what might be the problem?

Upvotes: 1

Views: 2982

Answers (1)

runrig
runrig

Reputation: 6524

LD_LIBRARY_PATH needs to be set before perl starts. Write a shell wrapper that sets the variables and then launches your script.

Also see this thread for alternatives and other links to explanations.

Upvotes: 2

Related Questions