Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Windows specific shell command calls

by sonic (Acolyte)
on Mar 26, 2004 at 06:14 UTC ( [id://339949]=perlquestion: print w/replies, xml ) Need Help??

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

Any reference for reading about Windows specific use of backticks versus system to execute shell commands?

Replies are listed 'Best First'.
Re: Windows specific shell command calls
by DamnDirtyApe (Curate) on Mar 26, 2004 at 06:17 UTC

    sonic, do you have some reason to suspect it behaves differently? I don't have a Windows box to test on, but I strongly suspect it does the same thing it does everywhere else.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Windows specific shell command calls
by bart (Canon) on Mar 27, 2004 at 01:06 UTC
    Backticks (and qx) capture the child program's STDOUT and return it. system leaves STDOUT alone. That's the difference.

    As for Win32, you might also reconsider using Win32::Process, as it allows you to not wait for a program to finish. It'll launch a program, any program, like the shell would do — in particular, itr's a good way to launch Windows programs (like notepad).

    Other interesting appoaches, that work well on Win32 too, include IPC::Open2, IPC::Open3, and IPC::Run. I'm sure I'm still missing a few more, interesting, options.

      Is there no way to do both--catch both the error code and the output of the called command?
        Well, you can use backticks/qx to capture the STDOUT, and check $? for the exit code — which agrees with what system returns ... check perlvar for details in $?.
Re: Windows specific shell command calls
by periapt (Hermit) on Mar 26, 2004 at 13:13 UTC
    I can't think of a reference beyond Learning Perl, Perl Cookbook. I primarily use one or the other depending on what I need from the called program. Backticks will capture the STDOUT of the invoked program while system will return the exit status of the program. If I want the output, I use backticks, if all I need is the exit status, I use system. System does have one advantage over backticks in that you can spawn independant processes with system using the start command on windows, for example system("start $somepgm"). However, with backticks, start will not spawn an independant process. The calling program will wait until the called program exits. David Roths Win32 Perl Programming: The Standard Extenstions chapter 8 Processes, touches on this briefly but otherwise doesn't say much about it. I know that there are other ways of spawning processes, (fork, Win32::Spawn etc), but I like to keep things as simple as I can and backticks / system have worked for me so far.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found