Setting LD_LIBRARY_PATH in Perl

Posted on 2011-04-29
Medium Priority
Last Modified: 2013-12-06
Hi All:

I want to set LD_LIBRARY_PATH in perl. I am doing the following:

$libpath = '/users/devtest/tmp/xoslib-';
$ENV{LD_LIBRARY_PATH} .= ':' . $libpath;

Please let me know how to make this work.

Best Regards,

Question by:sunnybrad
  • 2
LVL 48

Expert Comment

ID: 35495295
What you have looks correct.

What makes you think it isn't working?
LVL 40

Expert Comment

ID: 35495305
Likely you are having the problem that by the time your script needs LD_LIBRARY_PATH, the loader has already run.

So do this (from Perl monks http://www.perlmonks.org/index.pl?node_id=126575)

Essentially set it, then exec yourself.

my $interactive = TRUE if ( ! system( 'tty -s' ) / 256 );

unless ($interactive)
   unless ( $ENV{BEGIN_BLOCK} ) {
      $ENV{"LD_LIBRARY_PATH"} = '/users/devtest/tmp/xoslib-';
      $ENV{BEGIN_BLOCK} = 1;
      exec 'env',$0,@ARGV;

Open in new window


    my $need= '/usr/local/sybase/lib';
    my $ld= $ENV{LD_LIBRARY_PATH};
    if(  ! $ld  ) {
        $ENV{LD_LIBRARY_PATH}= $need;
    } elsif(  $ld !~ m#(^|:)\Q$need\E(:|$)#  ) {
        $ENV{LD_LIBRARY_PATH} .= ':' . $need;
    } else {
        $need= "";
    if(  $need  ) {
        exec 'env', $^X, $0, @ARGV;

Open in new window

Again, credit goes to perl monks (tye and ghodmode), just referencing the sample I've used.

Author Comment

ID: 35495326
Hi Tintin:

After the program runs I don't see the path getting set.

pot:110> env | grep LD_L

Please suggest a solution.

Best Regards

LVL 40

Accepted Solution

mrjoltcola earned 2000 total points
ID: 35495345
I did post a solution for that exact problem.

The issue is your Perl interpreter runs in its own environment. Once it exits, the environment goes back to the original shell's env.

If you want it permanently changed, add it to your .profile or /etc/profile

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Six Sigma Control Plans
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question