Whenever I set up something under cron, I always specifically source the
.profile or .cshrc I need, but that only gets the UNIX environment
variables into the shell.  You must do that, but it still doesn't
automatically get the UNIX environment variables into the perl script run
under cron -- or at least not in a simply accessible way.

I kinda got tired repeatedly coding that type of thing myself, and wrote a
small perl routine a while back that's been useful.  I suppose you could
put it in a BEGIN block, but I just call it early before doing anything
requiring one of the UNIX environment variables.

For some simple perl scripts, I pull in all the UNIX environment variables
so I don't have to worry about hard-coding specific variables or later
adding new ones.  No feedback about coding style please.  This was a quick
hack.  It is very likely there is a far more elegant way to do this, but it
works and wasn't worth any more effort.  I found using the $ENV{  } syntax
noisy in simple scripts.

This routine can be used to automatically pull in all UNIX environment
variables into main or just a specific list.  It assigns them to
correspondingly named perl variables in the main package.  NOTE: It won't
overlay any matching variable already existing in main -- which is probably
what you want -- and it avoids loading any environment variables that start
with "_", which are usually application specific variables you don't want
to use anyway.

Actually, I'd like to see if anyone has a cleaner way to do this.  Use it
at your own risk.

Example calls:

      &::imp_env_vars;   # Gets them all.

     or

      @::mylist = qw( HOME, USER );  # Pull in specific environment
variables, for example.
      &::imp_env_vars( @::mylist );

     so

      for example, the UNIX HOME variable could then be accessed as $::HOME
or $HOME, depending on whether you have used "use strict".

Here's the routine itself:

sub imp_env_vars
{
   my ( @inlist ) = @_ ;

   # If no input list specified, default to importing all UNIX environment
variables.
   scalar(@inlist) || ( @inlist = keys %ENV );

   no strict 'refs';
   for my $sym ( @inlist )
   {
      # Skip any environment variables prefixed with "_", which are
      # usually application specific variables.
      if ( $sym !~ /^\s*\_/ )
      {
         # Assign into main hash as simple scalar, only if not already
defined.
         if ( !defined(${"main\::$sym"}) )
         {
            # Critical assignment into main via glob.
            *{"$\::$sym"} = \$ENV{$sym};
         }
      }
   }
   use strict;
}


That's it.  Let me know if you found this useful.

      Bob Doucette
      Putnam Investments
      [EMAIL PROTECTED]




                                                                                       
                                          
                From:        Shiva kumar <[EMAIL PROTECTED]>                           
                                      
                       04/28/2004 11:44 PM                                             
                                          
                                                                                       
                                          
                                                                                       
                                          
                To:            [EMAIL PROTECTED], Bruce Shaw <[EMAIL PROTECTED]>       
                                      
                cc:            "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>               
                                        
                Subject:    Re: environment variables in perl scripts                  
                                          
                                                                                       
                                          



Hi ,

  The other alternative is.
  Define a BEGIN block in your perl program.
  BEGIN {
       $ENV{"VAR1"} = "Variable1";
       push @INC,"/newpath/you/interested/"; #PATH
definitions
  }

cu
Shiv

--- [EMAIL PROTECTED] wrote:
>
> Hi Bruce,
>
> You can include the variables in a shell script
> along with a call to your
> perl script, something like:
>
> #!/bin/sh
> #
> # setup env that ./setup_vars would have done:
> VAR1="blar1blar" export VAR1
> VAR2="blar2blar" export VAR2
> VAR3="blar3blar" export VAR3
>
> # setup PATH
> PATH=$PATH:/usr/local/bin:/somewhere/else export
> PATH
>
> # call your perl script
> cd /usr/local/bin/mine;
> ./myscript.pl & > diag.txt
>
> I personally don't think its too clever to source
> your .profile from cron.
> What happens in some months (or years) time when you
> or someone else
> changes it?
>
> Cheers
> Tony Adolph
>
>
>
>
>
>
>                       Bruce Shaw
>
>
>                       <[EMAIL PROTECTED]        To:
>     "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
>
>                       b.ca>                    cc:
>
>
>
> Subject:  environment variables in perl scripts
>
>                       24.04.2004 08:16
>
>
>
>
>
>
>
>
>
>
>
>
> I've got a working dbi script but I can only get it
> going live.
>
> If I run it as a crontab entry, it won't work
> because there's no
> environment
> variables.
>
> I've even tried something like:
>
> 12 23 * * * cd /usr/local/bin/mine; ./setup_vars;
> set > set.txt;
> ./myscript.pl & > diag.txt
>
> where setup_vars is a script that's supposed to set
> my variables.  I've
> also
> got them set in .profile, but when I examine
> set.txt, none of the
> environment variables are there and diag.txt shows
> that nothing has worked
> because the variables are missing.
>
> What am I doing wrong?
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system
> (http://www.grisoft.com).
> Version: 6.0.659 / Virus Database: 423 - Release
> Date: 4/15/2004
>
>
> This communication is intended for the use of the
> recipient to which it is
> addressed, and may contain confidential, personal
> and or privileged
> information. Please contact us immediately if you
> are not the intended
> recipient of this communication, and do not copy,
> distribute, or take
> action
> relying on it. Any communication received in error,
> or subsequent reply,
> should be deleted or destroyed.
>
>
>
>
>





__________________________________
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover




Reply via email to