Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am confused in system() and backticks commands. I have a perl script which executes another perl script via system command. I set some env variables on my shell(xterm) before launching the parent script.... I notice the following * If i call the the executed script via the system() function in the parent script the env variables which were set are passed to the executed script. * If i use the backticks instead, the env variables are not passed. As i am novice to perl, can someone pls explain the behavior Thanks in advance

Replies are listed 'Best First'.
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
      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.
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.
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.
      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.

      And that backticks return the command's output whereas system returns its exit value.

        Oh, yes, that too. I'd forgotten because I usually open a pipe if it's the output I'm after.
Re: system vs backticks
by tcf03 (Deacon) on Sep 08, 2005 at 14:29 UTC
    Merlyns article explains the whys sufficiently, but why use either system or backticks? use do to run a perl script from within a perl script.

    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
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`
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.)