Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set new environment variable for currient user sesion

Status
Not open for further replies.

1yura1

Programmer
Feb 7, 2003
36
0
0
UA
Hi All.

I need to set new environment variable value (or change an old one), not for the running script fork, but for currient user sesion.

Thanks,
Yura.
 
Environment variables are only local to the script execution. You can not pass them back and forth across http requests.

If you use real sessions (See one of the many excellent session management modules on CPAN) you can store values in the session and the session token CAN be passed.

Alternatively, you can set the env variable and then set a cookie in the user's browser indicating this env and its value. On each subsequent request you can read this cookie and use the value. I think this is a 'suboptimal' solution but is a quick and dirty hack that will work.
 
What if this wasn't running via CGI? Can Perl change the calling shell's environment?

________________________________________
Andrew - Perl Monkey
 
Perl runs within a shell, you can change perl's environment but once the session is over (the http request) the env is lost as the shell exits.

What are you really trying to accomplish here?
 
Me? Absolutely nothing. I was wondering if there were a way to run a perl script and alter the calling environment. Say I want to be at a shell and have $TERM be set to 'xterm' but after running some perl, that shell now sees $TERM as 'cons25'. Absolutely nothing to do with CGI. This was my interpretation of the OP's question.

________________________________________
Andrew - Perl Monkey
 
Env vars are invoked at shell startup.

Perl can change its own shell env.

You can use perl to alter your dotfiles for the shell you want to manipulate environment variables for. This will enabled the next person to login for the account that you modified dotfiles for to get your new values.

Can you modify the env for every currently open shell on the system? nope.

 
Have a look at Storable and getpwuid or Win32::LoginName(), if you want to have a persistant session for a specific user. Some example code:

*nix:
Code:
sub UserName {
    return getlogin || getpwuid( $< ) || $ENV{ LOGNAME } || $ENV{ USER } ||
        $ENV{ USERNAME } || 'unknown';
}
Win32:
Code:
use Win32;
sub UserName {
	return Win32::LoginName();
}

Barbie
Leader of Birmingham Perl Mongers
 
Thanks for help.

Perl cannot change/set 'wide' system enviroment variables.

The only way to do this for all new sesions - create exporting files/scripts for different shells and plase them into '/etc/profile.d/' (for Linux).

Thanks again.
Yura.
 
Thats what I said...

Its always funny when someone who starts a thread ends it as if they knew it all along :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top