Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Perl 'global' variables

Posted on 1997-11-02
10
615 Views
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?
0
Comment
Question by:myleslawrence
  • 5
  • 3
  • 2
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1208043
How about in a file with a name unique to the session?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 1208044
if your environment contains $USER

$user = $ENV{'USER'};
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208045
Oh, was the question meant to be "where can I find", rather than
"where can I put"?

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 1208046
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

BTW
ozo, am I right that you also don't know any other way to export environment variables in perl?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208047
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1208048
> 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";'


0
 

Author Comment

by:myleslawrence
ID: 1208049
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1208050
How about  $ENV{'USERNAME'}  on NT?
0
 

Author Comment

by:myleslawrence
ID: 1208051
Nope - nothing
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1208052
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl DBI Transactions Using Custom Module 7 48
Perl Awk Need Help 3 128
To run CGI/perl on tomcat and connect to oracle database. 5 136
Perl tar error 8 66
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question