Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Set environment variable for one time system command windows

by rmahin (Beadle)
on Mar 27, 2013 at 18:32 UTC ( #1025784=perlquestion: print w/ replies, xml ) Need Help??
rmahin has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, have a question that I hope is easy to answer.

I have multi-threaded server application that needs to set an environment variable, and then execute a command, but not set it for the rest of the threads. Here is an example that shows something similar to what I am doing,

use strict; use warnings; if($^O =~ /mswin32/i){ system("set test=hello && echo %test%"); }else{ system("test=hello; export test; echo \$test"); }

The unix portion of the code works flawlessly, the windows portion however just outputs '%test%'. I have tried using ';' and '&' in addition to the '&&' all with no success. is there a way to accomplish what I'm trying to do?

Thanks.

Comment on Set environment variable for one time system command windows
Download Code
Re: Set environment variable for one time system command windows
by BrowserUk (Pope) on Mar 27, 2013 at 19:10 UTC

    The problem is that the variable substitution %test% is done before the command is executed. Ie. the variable in the echo is expanded before the set is exectuted.

    However, if the second command is an executable rather than a built-in command, the variable is set when the executable runs:

    C:\test>set test= & perl -E"system q[set test=fred & perl -E\"say $ENV +{test}\"]" fred

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Well damn haha, just assumed that since the echo wasn't working the variable was not getting set. Thanks for clarifying.
Re: Set environment variable for one time system command windows
by aitap (Deacon) on Mar 27, 2013 at 19:41 UTC
    Since running a command requires a separate process, fork it and set the environment variable via %ENV, then exec your program:
    if (fork == 0) { local $ENV{myvariable} = "test"; exec("echo ".($^O eq "MSWin32" ? "%myvariable%" : '$myvariable')) or +die "exec: $!\n"; } __END__ text
    Sorry if my advice was wrong.
Re: Set environment variable for one time system command windows
by samwyse (Scribe) on Mar 27, 2013 at 19:55 UTC

    In both branches of your if statement, your string contains meta-characters, but on unix the shell expands environment variables as each statement is executed, while on windows they are only expanded once when the line is read. On my windows server, this is what happens when I run your command:

    R:\>set test=hello && echo %test% %test%
    I need to invoke the shell once more to make it work.
    R:\>set test=hello && cmd /c "echo %test%" hello

    This should work:

    use strict; use warnings; if($^O =~ /mswin32/i){ system(qq!set test=hello && cmd /c "echo %test%"!); }else{ system("test=hello; export test; echo \$test"); }
      This is basically what BrowserUK said as well, thanks for the extra verification

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025784]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2014-08-21 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (142 votes), past polls