Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: System command silent screen

by afoken (Chancellor)
on Oct 10, 2010 at 19:18 UTC ( [id://864506]=note: print w/replies, xml ) Need Help??


in reply to Re^4: System command silent screen
in thread System command silent screen

No, it isn't perfect. It invokes an unknown shell with unquoted arguments. That's begging for trouble.

If you want your script to play safe, fork() manually, then open /dev/null (or even better the return value of File::Spec->devnull()) as STDOUT and STDERR in the child process, then call exec() with a list of program name and arguments. In the parent process, wait() or waitpid() for the PID returned by fork().

This way, no shell is involved, and all of those nasty quoting problems are magically gone.

Of course, this requires more typing and more thinking. Alternatively, you can use one of the CPAN modules that wraps open(), fork(), exec(), waitpid(). Look at IPC::Run and IPC::Run3.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^6: System command silent screen
by ikegami (Patriarch) on Oct 10, 2010 at 21:20 UTC

    No, it isn't perfect. It invokes an unknown shell with unquoted arguments. That's begging for trouble.

    If there is a non-zero number of arguments, the following remove the need for quoting:

    use IPC::Open3 qw( open3 ); { open(local *FR_NULL, '<', '/dev/null') or die; open(local *TO_NULL, '>', '/dev/null') or die; my $pid = open3('<&FR_NULL', '>&TO_NULL', undef, $command, @arguments); waitpid($pid, 0) or die; }

      I nevertheless prefer afoken’s solution, because it would be a general one.   (Also, as noted, it is something that has probably already been done.)

      In my book, it is well worth the “extra effort” to develop a solution that makes a problem go-away completely, in every case, and never come back under any (unforseen at the time it was written) circumstances.   (The “suh-prize!”-es usually pop up at three o’clock in the morning.)   Best to “whack that mole” such that it stays whacked.

        I nevertheless prefer afoken’s solution

        Using fork and exec and pipe and dup2 yourself? You're talking about a substantial amount of code. (50 lines?) That's the solution of his I was replacing.

Re^6: System command silent screen
by chuckbutler (Monsignor) on Oct 10, 2010 at 23:52 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found