in reply to using .profile in perl

You have given us no information about what is in the ./home/apps/profile and how you are calling, but I presume that it contains bash variable assignments (export FOO='bar') and you are using system() or exec() to call it.

These get run in a separate process which runs and exits and does not affect your currently running process.

But it seems silly to use bash to set environment variables from a perl script, when you can do $ENV{FOO} = 'bar'.

You can just open the file, read it in line by line and assign the values to keys of %ENV.

Also, why are you using environment variables? I could understand it maybe if you were possibly calling a non-perl program from your script, but if this is all in perl, why not load your config variables using YAML or YAML::Syck?

use YAML(); our %Config = YAML::LoadFile('./home/app/profile');
and in ./home/app/profile:
--- foo: bar arrayref: - 1 - 2 - 3 hashref: foo: bar baz: bar
Clint

Replies are listed 'Best First'.
Re^2: using .profile in perl
by Anonymous Monk on Feb 20, 2006 at 02:29 UTC
    ok here's some background on the current situation: We have 2 instances of our application setup on 1 server, but using different ports to use the application. So, rather than having two different logins for each instance, we have created 2 different .profile (ofcourse they both contain different names) The .profile basically sets up the environment values for each instance.
    I have a perl script that needs to be run on both instances..but I want to just have 1 version of the script, rather than duplicate the script on both instances. Also, some of the code within script is interrelated...for eg. I may want to call another script within this one, and use that information to do something else on the other instance. That is the reason, I want to source the .profile within the perl script...such that I want to change the environment half way through the perl script.
    I hope this helps in your understanding of our problem. Sorry if i'm doing a bad job at explaining, but I'm really new to scripting.

      You could do something like the following (using ENV rather than foo):

      use strict; use warnings; use Data::Dump::Streamer; my %foo = (a => 'b', c => 'd'); my @stack; my %newFoo = (x => 'b', y => 'd'); push @stack, {%foo}; %foo = %newFoo; print "Using \%newFoo\n"; Dump (\%foo); # do stuff with temporary foo %foo = %{pop @stack}; print "restored original \%foo\n"; Dump (\%foo);

      Prints:

      Using %newFoo $HASH1 = { x => 'b', y => 'd' }; restored original %foo $HASH1 = { a => 'b', c => 'd' };

      DWIM is Perl's answer to Gödel