Clicky

Hi ALL,


I would like to source an Environment file in my Perl Script.
I have an Env file which has a lot of variables which i need to pass to my perl script. Is there a way we can just say.

System "source /home/cindi/bashrc"


I am stuck here,

Thanks

asked 07/22/2010 06:28

cyindi's gravatar image

cyindi ♦♦


11 Answers:

Not really.  You can use config files of various flavors (see the various config modules).

The other option is to process it manually - looping over the file and parsing the lines (which are hopefully consistent and simple).

link

answered

wilcoxon's gravatar image

wilcoxon

Short answer is no.

If your environment variables are already set (as they would be with .bashrc), then you can access them in perl like

$ENV{HOME}

link

answered 2010-07-22 at 14:34:09

Tintin's gravatar image

Tintin

Well i have to source different Environment variables depending upon the  input argument  for my perl script

For Eg:
 perl mybackupscript.pl A


if the input is A then A's DB Source file to export to Env variables
else if input is B then B's DB  Source file.
else if  input is C then C's DB Source File.



I  case use case if it is supported in Perl. But i need to source atleast 10 Env variables at a time and
System "source doesnot work"

link

answered 2010-07-22 at 14:35:20

cyindi's gravatar image

cyindi

Is there some reason you can't source the file before calling the perl script?

Basically, write a shell script that looks something like this:

#!/bin/bash
if [ $1 = 'A' ]; . A.env
elif [ $1 = 'B' ]; . B.env
fi
perl mybackupscript.pl

Then in mybackupscript.pl, you just reference the variables as $ENV{variable_name}.

link

answered 2010-07-22 at 14:42:08

wilcoxon's gravatar image

wilcoxon

Yes, i Just had a look  at the Env::Sourced module and installed it successfully. This is an easy way to source the Env file.

http://search.cpan.org/~jmcada/Env-Sourced-1.00/lib/Env/Sourced.pm-orig#NAME


use Env::Sourced qw(bashrc);

link

answered 2010-07-22 at 14:53:07

cyindi's gravatar image

cyindi

I hadn't heard of Env::Sourced before.  Looking at CPAN, it looks like it is unsupported (1.0 was released in 2005 with no updates since) and buggy.  The perldoc lists one major bug (unability to parse "export var=value" on some systems) and the testers reports show that the module only works on perl 5.8.x (not 5.6 nor 5.10).

Given the above, I would recommend *not* using the module.

link

answered 2010-07-22 at 15:38:16

wilcoxon's gravatar image

wilcoxon

ok, I would go with your advice.

my $varfile = "bashrc";


open(FH,"$varfile") or die "Couldn't open $varfile: $!\n";
while (<FH>) {
  chomp;
print "$_\n";
  }
close FH;

I was wondering if there is any way for redirecting the contents for the bashrc file after reading and sourcing each export variable.

The bashrc file looks like this.

export AWS_ACCESS_KEY_ID=aaa
export AWS_SECRET_ACCESS_KEY=bbb
export EC2_PRIVATE_KEY=aa.pem
export EC2_CERT=bb.pem
export EC2_HOME=/usr/local/ec2
export PATH=/usr/local/ec2/bin:$PATH
export JAVA_HOME=/usr
export PATH=$PATH:/usr/bin/java
export EC2_URL=https://aws.amazon.com

link

answered 2010-07-22 at 15:52:00

cyindi's gravatar image

cyindi

What do you mean by redirecting?

If you mean actually setting the variables in perl then this should work...

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
my $varfile = "bashrc";

#commented out open and close for now
#DATA is a special filehandle that reads __DATA__
#open(FH,"$varfile") or die "Couldn't open $varfile: $!
";
while (<DATA>) {
  chomp;
  if (/^exports+(w+)=(.*?)s*$/) {
    # you could put the values in any hash
    # I thought %ENV was appropriate considering where they come from
    $ENV{$1} = $2;
  } else {
    warn "could not process line $.:
$_
";
  }
}
#close FH;
__DATA__
export AWS_ACCESS_KEY_ID=aaa
export AWS_SECRET_ACCESS_KEY=bbb
export EC2_PRIVATE_KEY=aa.pem
export EC2_CERT=bb.pem
export EC2_HOME=/usr/local/ec2
export PATH=/usr/local/ec2/bin:$PATH
export JAVA_HOME=/usr
export PATH=$PATH:/usr/bin/java
export EC2_URL=https://aws.amazon.com
link

answered 2010-07-22 at 16:04:37

wilcoxon's gravatar image

wilcoxon

Hi,

I was able to get through the Env file. Thanks for the solutions you all.


Thanks

link

answered 2010-07-22 at 21:12:42

cyindi's gravatar image

cyindi

Another way would be to use system to process the file, then (before exiting the shell) have the shell display all of the env vars.  This will be more reliable in more complex cases.

1:
2:
3:
4:
5:
my @Output = `$varfile > /dev/null; env`;
foreach (@Output) {
    next unless /(.*?)=(.*)/;
    $ENV{$1}=$2;
}
link

answered 2010-07-26 at 10:50:50

Adam314's gravatar image

Adam314

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

link

answered 2010-09-08 at 21:04:30

suhasbharadwaj's gravatar image

suhasbharadwaj

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×1

Asked: 07/22/2010 06:28

Seen: 429 times

Last updated: 12/17/2011 05:20