Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to set LD_LIBRARY_PATH

887 views
Skip to first unread message

newbie01 perl

unread,
May 28, 2010, 3:45:14 AM5/28/10
to beginners, dbi-users
Hi all,

Can someone advise how to set LD_LIBRARY_PATH from within the Perl scripts?

If I set LD_LIBRARY_PATH from the command line, all is okay

[oracle ~]$ perl -e 'use DBD::Oracle; print $DBD::Oracle::VERSION,"\n";'
Can't load
'/oracle/product/db/11.1/perl/lib/site_perl/5.8.3/x86_64-linux-thread-multi/auto/DBD/Oracle/Oracle.so'
for module DBD::Oracle: libclntsh.so.11.1: cannot open shared object file:
No such file or directory at
/usr/lib64/perl5/5.8.5/x86_64-linux-thread-multi/DynaLoader.pm line 230.
at -e line 1
Compilation failed in require at -e line 1.
BEGIN failed--compilation aborted at -e line 1.
[oracle ~]$ export LD_LIBRARY_PATH=/oracle/product/db/11.1/lib
[oracle ~]$ perl -e 'use DBD::Oracle; print $DBD::Oracle::VERSION,"\n";'
1.15

But if I do the following instead in the Perl script, it does not work? How
to set the LD_LIBRARY_PATH then?

$ENV{ORACLE_HOME}=$ORACLE_HOME;
$ENV{ORACLE_SID}=$ORACLE_SID;
$ENV{PATH}="$ORACLE_HOME/bin:$PATH";
$ENV{LD_LIBRARY_PATH}="$ORACLE_HOME/lib";

FYI, the script is to run from a cron which is why am setting
LD_LIBRARY_PATH in the script.

Any response will be very much appreciated. Thanks in advance.

Shlomi Fish

unread,
May 28, 2010, 4:06:41 AM5/28/10
to begi...@perl.org, newbie01 perl, dbi-users
On Friday 28 May 2010 10:45:14 newbie01 perl wrote:
> Hi all,
>
> Can someone advise how to set LD_LIBRARY_PATH from within the Perl scripts?
>
> If I set LD_LIBRARY_PATH from the command line, all is okay
>
> [oracle ~]$ perl -e 'use DBD::Oracle; print $DBD::Oracle::VERSION,"\n";'
> Can't load
> '/oracle/product/db/11.1/perl/lib/site_perl/5.8.3/x86_64-linux-thread-multi
> /auto/DBD/Oracle/Oracle.so' for module DBD::Oracle: libclntsh.so.11.1:

> cannot open shared object file: No such file or directory at
> /usr/lib64/perl5/5.8.5/x86_64-linux-thread-multi/DynaLoader.pm line 230.
> at -e line 1
> Compilation failed in require at -e line 1.
> BEGIN failed--compilation aborted at -e line 1.
> [oracle ~]$ export LD_LIBRARY_PATH=/oracle/product/db/11.1/lib
> [oracle ~]$ perl -e 'use DBD::Oracle; print $DBD::Oracle::VERSION,"\n";'
> 1.15
>
> But if I do the following instead in the Perl script, it does not work? How
> to set the LD_LIBRARY_PATH then?
>
> $ENV{ORACLE_HOME}=$ORACLE_HOME;
> $ENV{ORACLE_SID}=$ORACLE_SID;
> $ENV{PATH}="$ORACLE_HOME/bin:$PATH";
> $ENV{LD_LIBRARY_PATH}="$ORACLE_HOME/lib";
>
> FYI, the script is to run from a cron which is why am setting
> LD_LIBRARY_PATH in the script.
>

If you're going to use "use DBD::Oracle;" in the script then you'll need to
put these statements in a BEGIN block:

[code]
use vars qw($ORACLE_HOME $ORACLE_SID);

BEGIN
{
$ORACLE_HOME = ...;
$ORACLE_SID = ...;


$ENV{ORACLE_HOME}=$ORACLE_HOME;
$ENV{ORACLE_SID}=$ORACLE_SID;
$ENV{PATH}="$ORACLE_HOME/bin:$PATH";
$ENV{LD_LIBRARY_PATH}="$ORACLE_HOME/lib";
}

use DBD::Oracle;
[/code]

That's because "use" is executed at compile-time instead of run-time.

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Paul Johnson

unread,
May 28, 2010, 12:33:31 PM5/28/10
to John Scoles, newbie01 perl, beginners, dbi-users
On Fri, May 28, 2010 at 06:14:38AM -0400, John Scoles wrote:

> You will have to set those values before your modules load.
>
> So you should stick them in the BEGIN and that should work

... except where it doesn't, such as on Solaris for example. Here,
LD_LIBRARY_PATH (at least) really does need to be set before the process
starts. You can do this by writing a shell wrapper, or re-execing your
perl script if the value is not already set.

You're using linux and I'm not sure if this sort of messing about is
required there.


> http://www.compuspec.net/reference/language/perl/BEGIN_and_END.shtml
>
> cheers
> John Scoles

> --
> Catch Alex & Sheeri at ODTUG/Kaleidoscope - June 27 - July 1.
> Hear Sheeri speak or email eve...@pythian.com to meet with Pythian.

--
Paul Johnson - pa...@pjcj.net
http://www.pjcj.net

Marilyn Sander

unread,
May 28, 2010, 2:23:32 PM5/28/10
to Paul Johnson, John Scoles, newbie01 perl, beginners, dbi-users

Have you considered doing a require instead of a use. With require, the loading is done at run time, and would be governed by the setting of LD_LIBRARY_PATH, at the time the require statement is executed. Just set LD_LIBRARY_PATH before doing the require.
--Marilyn


Paul Johnson

unread,
May 29, 2010, 9:24:12 AM5/29/10
to Marilyn Sander, John Scoles, newbie01 perl, beginners, dbi-users
On Fri, May 28, 2010 at 11:23:32AM -0700, Marilyn Sander wrote:

> On May 28, 2010, at 9:33 AM, Paul Johnson wrote:
>
> > On Fri, May 28, 2010 at 06:14:38AM -0400, John Scoles wrote:
> >
> >> You will have to set those values before your modules load.
> >>
> >> So you should stick them in the BEGIN and that should work
> >
> > ... except where it doesn't, such as on Solaris for example. Here,
> > LD_LIBRARY_PATH (at least) really does need to be set before the process
> > starts. You can do this by writing a shell wrapper, or re-execing your
> > perl script if the value is not already set.

> Have you considered doing a require instead of a use. With require, the


> loading is done at run time, and would be governed by the setting of
> LD_LIBRARY_PATH, at the time the require statement is executed. Just set
> LD_LIBRARY_PATH before doing the require.

I'm afraid that you may have misunderstood what I wrote. There are
times when you really do need to set the variable before the process
starts.

Chas. Owens

unread,
May 30, 2010, 8:43:56 AM5/30/10
to Bobak, Mark, Paul Johnson, John Scoles, newbie01 perl, beginners, dbi-users
On Sat, May 29, 2010 at 12:19, Bobak, Mark <Mark....@proquest.com> wrote:
> I'd argue you want to use a shell wrapper anyhow, because rather than setting those variables explicitly, you ought to do something like:
> export ORACLE_SID=your_sid
> export ORAENV_ASK=NO
> . oraenv
>
> and Oracle will set it all for you, and if/when something changes or is upgraded, your code will continue to do the right thing.
snip

Yeah, using a wrapper shell script to setup the environment is always
a good idea. I have seen too many Perl scripts (and C programs) fail
miserably when run by cron because they assumed some environment would
be present. This would also be a good place to set PERL5LIB and any
other variables you need to exist.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

Marilyn Sander

unread,
May 30, 2010, 1:47:50 PM5/30/10
to Paul Johnson, beginners, dbi-users

I did not misunderstand what you wrote. My reasoning was that the
thing being
loaded is a shared object (.so file). The system loader (ld) has to
be invoked for loading
a shared object. That seems to me to require a separate process, with
an environment
stack inherited from the Perl process that invokes it. I was also
assuming that setting
an environment variable at run time would set the environment for the
Perl process
that is executing the Perl program. However, I did not test it. I
will test it and see what happens.
--Marilyn

0 new messages