1337John has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: System() in list mode?
by ikegami (Patriarch) on Oct 17, 2011 at 21:12 UTC

    First, it's not being called in list context. That means something else. I call this the multiple argument form.

    When you use the single argument form, system expects a shell command and will launch a shell to execute the command*, so

    system('echo $PATH');

    is the same as

    system('/bin/sh', '-c', 'echo $PATH');

    The shell expands $PATH, and in turn, does

    system('echo', '/foo:/bar');

    which is quite different than your

    system('echo', '$PATH');

    You must either use the shell

    system('echo $PATH');
    system('/bin/sh', '-c', 'echo $PATH');

    or you must do what it would do

    system('echo', $ENV{PATH});

    My code is supposed to set $PATH, then run a command on the same line, in the same shell.

    You should start by launching a shell, then!

    system('PATH=/foo prog arg');

    Or not. This is something you can do yourself quite easily.

    local $ENV{PATH} = '/foo'; system('prog', 'arg');

    * — As an optimisation, Perl avoids launching a shell when it's obvious that the shell isn't needed.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: System() in list mode?
by dd-b (Pilgrim) on Oct 17, 2011 at 22:13 UTC

    The very first response seems to me to tell you everything you need to know -- your code doesn't work because the multi-argument form of system() isn't invoking a shell, so your "$PATH" isn't getting expanded, so it's just printing the characters dollar-sign, pee, ay, tee, aitch.

    And I concur that you're being quite rude to people who you seem to hope will help you. Especially since it looks to me like you're not reading the responses carefully enough to benefit from them.

Re: System() in list mode?
by RichardK (Parson) on Oct 17, 2011 at 22:06 UTC

    BTW -- You can have spaces in linux paths :)

    You just have to escape them on the command line with \

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: System() in list mode?
by anneli (Pilgrim) on Oct 18, 2011 at 09:29 UTC

    For output I get:
    $PATH
    env: bash: No such file or directory

    Where's the issue here? This is the correct output; the first system didn't expand $PATH (which we all know we all know about now), and in the second, you changed $PATH to foo and I guess you don't have a binary called foo/bash! If your PATH contained bash, using env PATH=... will work just fine.

    Anne

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: System() in list mode?
by onelesd (Pilgrim) on Oct 17, 2011 at 21:13 UTC
    4. I successfully did this using system() in scalar context (one string arguement, ok it's not actually scalar but w/e).

    When you use system with LIST context the subsequent command is not run in a shell. When you use it with one scalar argument the command is run in a shell.

      Well yea... it uses execvp(). Which should work no?
        There is no shell in LIST mode so setting a PATH variable doesn't have the effect you are looking for. You need to provide full paths to files/binaries, such as /bin/bash.

        Edit:

        No need to be an a$$ to people taking time out of their lives to help you.