Re: system vs backticks
by McDarren (Abbot) on Sep 08, 2005 at 06:26 UTC
|
It seems that I'm becoming an advocate for [id://merlyn] articles, but hey.. it's well written, easy to understand, and it gives a good introduction to this very topic - so check this out :)
-- Darren
| [reply] |
|
|
I agree that merlyn's article is well-written and is easy to understand. However that article doesn't seem (at least to a low-flying monk) to address the OP's question, which is about the passing of environmental variables by system and by backticks. I therefore support Errto's request for code from the OP, as I would like to learn about this.
| [reply] [d/l] |
Re: system vs backticks
by Errto (Vicar) on Sep 08, 2005 at 05:04 UTC
|
That shouldn't generally happen. Here's a minimal example for illustration:
foo.pl:
$ENV{FOO} = 'bar';
system 'perl bar.pl';
print `perl bar.pl`;
bar.pl:
print "I got $ENV{FOO}\n";
When I invoke "perl foo.pl" from my terminal, I get:
I got bar
I got bar
So maybe what you're actually seeing is something different? Perhaps a bit of code would help. | [reply] [d/l] |
Re: system vs backticks
by jonadab (Parson) on Sep 08, 2005 at 10:10 UTC
|
The primary difference between system and backticks, as far as I am aware, is that backticks always call a shell to parse the command; whereas, system might not, if you pass it a list. However, why a shell would not be inheriting environment variables from its parent environment is something I cannot explain. I don't know that much about shells, and an any case you haven't said what operating system this is happening on, what shells are installed, what the default shell is for the user the script is running as, and so on and so forth. The best guess I can hazzard is that this has something to do with it. | [reply] |
|
|
| [reply] |
|
|
Oh, yes, that too. I'd forgotten because I usually open a pipe if it's the output I'm after.
| [reply] |
Re: system vs backticks
by tcf03 (Deacon) on Sep 08, 2005 at 14:29 UTC
|
| [reply] |
Re: system vs backticks
by samizdat (Vicar) on Sep 08, 2005 at 17:11 UTC
|
untested, but you actually should be able to pass ENV vars within backticks by chaining multiple assignments:
`BAR=123;export BAR;perl foo.pl`
| [reply] [d/l] |
Re: system vs backticks
by ambrus (Abbot) on Sep 09, 2005 at 10:22 UTC
|
I agree with Errto in that both backticks and system should preserve environment variables, so the difference is caused by something else.
If you can reproduce the phenomenon with some simple code,
you might want to post it here so that we can examine that and help.
Also, just to make sure, are you not passing some environment variable significant for the shell, like PS1?
If so, it may be that the backticked version of your code calls a shell which destroys that variable. (I can't tell without the code if this is the case.)
| [reply] |