Okay, it was a combination of chromatic's and japhy's suggestions which got me working. But it wasn't until I read every response that I understood all of the things that I was doing wrong.

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:

foreach $key (keys %hash) { $ENV{$key} = $hash{$key}; }
This works perfectly. But why doesn't chromatic's example work?

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:

#!/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);
and then call the script with parms:
./testScript.pl -engEnvVar APP_DIR=/usr/vendor/app/version/ -engEnvVar + TMP_DIR=/usr/tmp/
... etc.

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:

use strict; %ENV = ( foo => 'bar '); system ( 'printenv' );
... 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.

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


In reply to Re: setting env vars from hash fails for me by ccarden
in thread setting env vars from hash fails for me by ccarden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.