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

I have ported out a shell script to a Perl script. It is done but the dang thing has been wiggin ever since I wrote it (when run from crontab). Runs like a champ from command line. Ok, so its obvious. Something odd with the environment. I tried setting what seems to be the necessities in the BEGIN{} clause but to no avail. The script still didn't run properly from crontab. For completeness sake, the actuall problem is not the script. It is an executable that the script has to system(). The executable needed some oracle db info to run properly. Incidently this was the info I manually set in %ENV. Now, I am resorting to alternate methods of troubleshooting. Obviously the next step in this would be to see what the shell script environment used to run and to mimic that environment. The shell script sourced a global config file which really had a lot of what seemed to be nonsense stuff in it. Anyway, again, for the sake of completeness, I went ahead and decided to bite the bullet and 'source' this file in my perl script. Naturally, there is no actual 'source' type command in Perl so I resorted to searching for assistance here.

This is not a plea for help as much as it is a request for comments. Please look over my code. I would like some feedback to see if I can do anything more efficiently.

BEGIN { use Env; # Ok. This is a test. Lets see if I can import # (source) a shell environment list. my $CRONENV = "/u20/home/gvc/bin/run_cronenv"; open(SOURCE,$CRONENV) or die("Cannot read $CRONENV\n"); while ( <SOURCE> ) { chomp(); $_ =~ s/export //; if ( !/^#/ and /\W/ ) { (my $key, my $value) = split(/=/) if $_; $ENV{$key} = $value; Env::import($key); } } close(SOURCE); # Gotta set some environmental stuff really fast. ## NOTE: this was the stuff I had in here before ## the mimication (tm) of the shell env file above. $ENV{'ORACLE_HOME'} = "/u40/app/oracle/product/8.1.6"; $ENV{'ORACLE_SID'} = "CORPDB"; $ENV{'ORAENV_ASK'} = "NO"; $ENV{'LD_LIBRARY_PATH'} .= $ENV{'ORACLE_HOME'} . "/lib"; # while ( (my $key,my $value) = each %ENV ) { # print "$key=$value\n"; # } } # End BEGIN clause
sidenote: The environment file is used by other shell scripts. It looks similar to this:

# # global environment file # export PATH=/path0:/path1:/path2 export ORAENV_ASK=NO export YADA=YADAYADA export ANOTHER_VAR=something export dt=`date +%y%m%d` # comment here export LAST=blah
The only thing I am mildy concerned about is the $dt environment setting. Does anyone think this will adversely effect my script? I don't think it will but I am still not yet familiar with all those Perl caveats.

TIA guys.

----------
- Jim

Replies are listed 'Best First'.
Re: sourcing an external shell file
by Anonymous Monk on Jul 31, 2001 at 05:34 UTC
    Don't mean to skirt the issue, but why not schedule a shell-script?
    #!/bin/sh source envfile perlscript.pl
    Just to make life simpler... :)
      hahahaha....If you weren't an anonymous monk I'd ++ you here. ;) Sometimes, we just short the shell a bit too much.

      ----------
      - Jim