We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Perl 'global' variables

Medium Priority
Last Modified: 2012-05-04
Where can I put a global username variable so that all of my scripts during a session can access it?
Watch Question

Most Valuable Expert 2014
Top Expert 2015

How about in a file with a name unique to the session?
if your environment contains $USER

$user = $ENV{'USER'};

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Most Valuable Expert 2014
Top Expert 2015

Oh, was the question meant to be "where can I find", rather than
"where can I put"?

Sorry if there are several ways to interprete spoken language.
I read it as:
   where in a session can I put a global variable, so that all my (perl) scripts can access them.

If myleslawrence meant:
   where can I put a perl global variable, so that all other perl scripts can access them
ozo's solution is the choice

ozo, am I right that you also don't know any other way to export environment variables in perl?
Most Valuable Expert 2014
Top Expert 2015

You may be right, that interpretation just didn't occur to me.
I was mainly wondering what constitutes a "session".

And yes, in perl or any other language, there's no legal way to
force another process to change its environment variables without
its cooperation.  You can set what a child is born with when you
spawn it, but you can't set your parent's environment.
> but you can't set your parent's environment.
I still know what  source  and  .  are doing, and that it is not possible with any other script, but this was the final hint.
Thanks ozo.
Instead of handing over some points, here is a quick 'n dirty solution for perl (to be improved in many ways:-)

    exec perl -e '$ENV{"your_var"}="value"; exec "/bin/csh";'


This sounds like it would work on inix but:
(if your environment contains $USER
$user = $ENV{'USER'}; )
didn't return anything on my NT server.
My specific situation is that I have many people entering data into a defect tracking database. One of the fields is submitter. I'd like to populate that field with their name but even if they 'login', I'd still have to pass the name from script to script. I'd rather just grab it from a global area. A random file name wouldn't work either because the next script wouldn't know what that file name was either. I must be missing something because I can't possibly be the first person who would want to do this
How about  $ENV{'USERNAME'}  on NT?


Nope - nothing
Hmm, sounds that your are not a NT-domain user.
Check in a cmd-window with  SET  which environment variables are set. Luckily you find one containing your username :-)
If not you have to modify your logon script to create such a variable.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.