in reply to setting env vars from hash fails for me
First, though, chromatic suggested %ENV{ keys %hash } = values %hash; which I tried right away, only to get a syntax error. Not certain what was failing, I went to japhy's suggestion and came up with:
This works perfectly. But why doesn't chromatic's example work?foreach $key (keys %hash) { $ENV{$key} = $hash{$key}; }
As for all of the other suggestions, they mostly had to do with my clumsy creation of the hash. My example code was significantly simpler than what I'm really doing, and I was trying provide you with a simple hash for testing. It was the use Env qw(keys %hash); that was really the hack, and it turns out that I misread an example somewhere on this site that was meant for multi-valued vars (thanks for the heads-up, PodMaster).
My real code takes in the hash as a parm using Getopt and then passes it to a sub intended to set the environment vars to their corresponding values in the hash:
and then call the script with parms:#!/usr/bin/perl use strict; use warnings; use Getopt::Long; my %engEnvVars; GetOptions ( "engEnvVar=s" => \%engEnvVars ) sub setEnvVars { my %engEnvVars = @_; my $key; foreach $key (keys %hash) { $ENV{$key} = $hash{$key}; } system("printenv") if $DEBUG; } setEnvVars(%engEnvVars);
... etc../testScript.pl -engEnvVar APP_DIR=/usr/vendor/app/version/ -engEnvVar + TMP_DIR=/usr/tmp/
Yes, dws was correct in that I am setting these environment variables so that I can call another application.
I tested chromatic's second example:
... and found that in that case printenv only prints out the one environment variable and its value. In my main script, printenv prints out not only the vars that I set in my script, but all other environment vars. Curious.use strict; %ENV = ( foo => 'bar '); system ( 'printenv' );
What a long, arduous path for me. Finding this site and sharing my questions shows me how little I really know about Perl. I appreciate all of your input. Thank you.
-- ccarden
|
|---|