For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2004 > How to load environment ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to load environment ?
Max

2004-05-15, 11:30 am


Hi,

I have a bash file that loads some variables needed by some perl scripts, I
need the environment to be available at any moment, so i included the line
that loads the environment in my .bash_profile, but when the perl scripts
runs from the crontab, the environment is not available.

So i use another bash script to load the environment and run the perl script
from one line in the crontab, the content of the load and run script is this:

-----------------------------------------------------------------------------------
!/bin/sh
.. ~max/tmp/g_amb.sh # <-- This Loads the environment.
/home/max/tmp/l_amb.pl # <-- This uses the environment loaded.
-----------------------------------------------------------------------------------

How could i load the environment from the perl script without using the load
and run script ?

Some perl or bash scripts are going to use the environment variables and i
dont want to define the variables on every script,

this is a sample content of the script that loads the enviroment:

-----------------------------------------------------------------------------------
#!/bin/sh

PVAR="Var P"
XVAR="Var X"

export PVAR XVAR
-----------------------------------------------------------------------------------

thanks

saludos
max

Paul Johnson

2004-05-15, 12:30 pm

On Fri, May 14, 2004 at 07:59:40PM -0500, max wrote:

> How could i load the environment from the perl script without using the load
> and run script ?


Check out Shell::Source. http://search.cpan.org/dist/Shell-Source/

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
David Storrs

2004-05-16, 8:30 am

On Fri, May 14, 2004 at 07:59:40PM -0500, max wrote:
[...]
> So i use another bash script to load the environment and run the perl script
> from one line in the crontab, the content of the load and run script is this:
>
> -----------------------------------------------------------------------------------
> !/bin/sh
> . ~max/tmp/g_amb.sh # <-- This Loads the environment.
> /home/max/tmp/l_amb.pl # <-- This uses the environment loaded.
> -----------------------------------------------------------------------------------
>
> How could i load the environment from the perl script without using the load
> and run script ?
>
> Some perl or bash scripts are going to use the environment variables and i
> dont want to define the variables on every script,



Max,

It sounds like you have a perfectly good solution. Quite honestly, I
would stick with it. Here's why:

First off, you've got two factors working together here:

1) Multiple Perl scripts must be able to see these variables.

2) At the time the Perl scripts are run, the environment is not
normally available.

#1 says that you need to centralize the variables somewhere so that
multiple scripts can load them up.

#2 says that you are going to need to take some explicit action to
instantiate the variables.



If you don't like that, then something like the following might work
(not tested):

-------------- File MyEnv.pm in @INC
package MyEnv;
use Env;

$ENV{PVAR} = 'p var';
$ENV{XVAR} = 'x var';
-------------- End


-------------- Begin script file:
#!/usr/bin/perl

use MyEnv;
-------------- End

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com