http://www.perlmonks.org?node_id=840447

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

I have a strange scenario (ooh missus)... When logged on as a super user, the only way I can connect to a database is by issuing the unset LIBPATH command before running my script. I have tried numerous ways to replicate this command within my script such as
1/ $command = 'unset LIBPATH'; system( $command );
but get the error Can't exec "unset": A file or directory in the path name does not exist.
$ENV{LIBPATH} = ""
and
$ENV{LIBPATH} = '/usr/lib:/lib:/home/db2inst1/sqllib/lib'
and
delete $ENV{LIBPATH};
and
system("LIBPATH=/usr/lib:/lib:/home/db2inst1/sqllib/lib; export LIBPAT +H");
and
$ENV{LIBPATH} = $ENV{ LIBPATH } . ":/lib:/home/db2inst1/sqllib/lib";
Whilst these appear to have set the $ENV{LIBPATH} correctly, I still get the error message
DBI connect('OLTTST','joltdev',...) failed: Total Environment allocati +on failure! Did you set up your DB2 client environment? at /apps/bin +/modules/jolt.pm line 88 DBI::__ANON__[/usr/opt/perl5/lib/site_perl/5.8.2/aix-thread-mu +lti/DBI.pm:658]('undef','undef') called at /usr/opt/perl5/lib/site_pe +rl/5.8.2/aix-thread-multi/DBI.pm line 660 DBI::connect('DBI','dbi:DB2:OLTTST','joltdev','joltuser') call +ed at /apps/bin/modules/jolt.pm line 88 jolt::connect() called at /apps/bin/optus/reconcile_withdotsh. +pl line 122 jolt::connect(/apps/bin/modules/jolt.pm:91):
The ONLY thing that seems to allow me to connect is running the 'unset LIBPATH' command before I run my script. Now I appreciate that I'm not supplying a whole heap of code here, but I just wonder if anyone has encountered a similar problem wherein they can connect as themselves, but not as a super user unless they have reset the libbath?

To be honest having tried whatever I can think of I haven't a clue!

I'd be grateful if anyone could suggest even the most outlandish resolution. Thanks Kev

Replies are listed 'Best First'.
Re: replicating the command 'unset LIBPATH' in perl
by ikegami (Patriarch) on May 18, 2010 at 04:52 UTC

    system('unset LIBPATH'); doesn't work since unset is a shell command, not a program.

    system(q{sh -c 'unset LIBPATH'}); would run the shell command, but it would uselessly change the environment of a shell that exits immediately afterwards.

    You want to change the current process's environment. delete $ENV{LIBPATH}; is the way to go. If it doesn't work, maybe because it's being done too late. Try putting it in a BEGIN {} block as near the top of your script as you can.

      Thanks for pointing out that unset is a shell command. I come from a mainframe background (please don't laugh) and will readilly admit to getting thoroughly confused between the various shells etc. I had tried the
      delete $ENV{LIBPATH}
      and it's right near the top of script.

      The weird thing is, that whilst it does replicate the unset command on going into the called routine, unlike the unset command it still causes a 'Total Environment allocation failure'.

      I realize this is now getting off of perl per se and more into shell commands - but I beg your indulgence, does 'unset LIBPATH' do anything other than delete the path?

      I did a comparison of env before and after issuing the command, and aside of the LIBPATH no longer being present, the only other difference was addition of the phrase "SHLVL=1".

      Appreciate your time in responding.

        I had tried the delete $ENV{LIBPATH} and it's right near the top of script.

        You missed the part about using BEGIN. The idea is to execute it before any use statements are executed, and you'll need BEGIN for that.

        BEGIN { delete $ENV{LIBPATH} }