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

Using the subroutine get_login_env that was recommended to me in an earlier post I'm now having qualified success with my script. I think I understand the subsequent problem that I'm having and I hope that one of the Monastery's boffins can confirm my suspicion. My code has the following -
#!/usr/bin/perl -w # use strict ; # sub get_login_env { local %ENV; my $shell = shift || (getpwuid($<))[8]; my $env = `echo env | perl -e 'exec {"$shell"} -sh'`; if (wantarray) { my @pieces = ($env =~ m/^(.*?)=((?:[^\n\\]|\\.|\\\n)*)/gm); s/\\(.)/$1/g foreach @pieces; return @pieces; } else { return $env; } } # ############################################################### # Set up variables # ############################################################### # my $ftpfile = 'mailtest.pl' ; my $out_file = 'ronniesrubbish' ; my $result = 0 ; my $key = undef ; my $value = undef ; # print "\n\t\t<***** xxrcenv Starts *****>\n" ; # %ENV = (%ENV, get_login_env()); # while (($key, $value) = each (%ENV)) { print "\n\tKey :: $key" ; print "\n\tValue :: $value" ; } # print "\n" ; # use lib '/home/interface/sectran/lib' ; #use lib "$ENV{SECTRAN_DIR}/lib" ; foreach (@INC) { print "\t$_\n" ; }
This now works but originally the use lib was worded differently - use lib "$ENV{SECTRAN_DIR}/lib" ; This generated a compilation time error. Is this because the use lib is parsed at compile time and the SECTRAN_DIR variable is created at run time and therefore doesn't exist when the use lib is evaluated? Just trying to clear this up in my head. Cheers, Ronnie

Replies are listed 'Best First'.
Re: UNIX/profile the sequel
by nimdokk (Vicar) on Nov 18, 2004 at 16:02 UTC
    It sounds like you might want to have the $ENV{SECTRAN_DIR} set in a BEGIN block, that way you should be able to have access to it with with use lib.
Re: UNIX/profile the sequel
by Happy-the-monk (Canon) on Nov 18, 2004 at 16:04 UTC

    he use lib is parsed at compile time and the SECTRAN_DIR variable is created at run time and therefore doesn't exist when the use lib is evaluated?

    That's logical. Have you tried a BEGIN block around the environment parser code to steal a little code evaluation time at compile time?

    Cheers, Sören

Re: UNIX/profile the sequel
by graff (Chancellor) on Nov 19, 2004 at 05:17 UTC
    As I mentioned in my reply to your ealier post, I wasn't sure about the relative ordering of "use" and "BEGIN", until I tried a little experiment. Base on the results, it looks to me like perl deals with "use" directives before it does the BEGIN block. So you need to make sure that the shell that invokes your perl script already has the environment set up for finding the module(s) you need to "use".

    I suppose there might be a work-around by loading your module with "require" instead of "use", since "require" is handled at run-time. Assuming that "require" searches @INC at run-time just like "use" does at compile-time, this should get you where you want to be without changing your cron job spec.

    Still, I'd be more inclined to change the cron job spec, so that the cron job shell gets the intended .profile environment before it runs the perl script.