Tuesday, July 28, 2009

In perl, how can I make a system() call without displaying the output?

For example, I have something like this:





system("copy $copySrc $copyTgt")





I check the return value to make sure the call succeeded. The only problem is, I get a line of output on my terminal saying:





1 files(s) copied.





Is there any way I can just ignore that output and not have it show up on the screen?

In perl, how can I make a system() call without displaying the output?
One way of discarding the output would be to use backticks [1].





`copy $copySrc $copyTgt`





As always, be wary of what is in the variables you’re using in system calls [2][3].








Perhaps a more robust option might be to use a module like IO::CaptureOutput [4], in which case your example would look more like:





my ($output, $error) = capture_exec('copy', $copySrc, $copyTgt);





You could then check $? for the system call’s status. I’ve created a sample program at http://pastie.caboo.se/99500 to illustrate the solution to your copy example.








Best wishes,


Dave Cardwell.


http://davecardwell.co.uk/perl/
Reply:If you want to copy files, use the File::Copy module.


If you want to capture system output, use backticks instead of system and redirect STDERR to STDOUT:





my $output = `command arg1 arg2 2%26gt;%26amp;1`;


No comments:

Post a Comment