What Hue-Bond said is correct.  Using the %ENV hash is the best and simplest way to set environment variables.

It's worth pointing out that setting environment variables in a Perl script, however, will not change your shell's environment variables.  That's because you are running Perl in a subprocess, which has its own environment variable list; as soon as the Perl script exits, you're back with the environment variables that the parent had originally.

To test this is as easy as running the following script:

#!/usr/bin/perl -w use strict; use warnings; $ENV{'abc'} = 'def'; system('echo $abc');
When you run this, you should see def printed.  But when you're back to your shell/prompt, (assuming variable abc wasn't defined in the first place):
% echo $abc abc: Undefined variable.
That's because:
[Parent process] ======> [Perl subprocess] ====> [Sub process shell] Env list #1, Env list #2 Env list #3 "abc" NOT defined copied from the copied from the Parent process, Perl subprocesses, define "abc" here "abc" still defined
So, when you've set the environment variable abc in the Perl subprocess, it is only altering the copy of the environment list inherited from the parent, not the parent's environment.

Moral of the story is:  there is no way that you can ever set an environment variable in a subprocess and have it "persist" to the parent process.

Update:  And the same thing is true, of course, for the process' notion of current directory.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Perl & Cshell by liverpole
in thread Perl & Cshell by shreedara

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.