GZaidman
GZaidman

Reputation: 797

command output is directed to the screen

novice Perl programmer here!. I'm using the system() function to get the return code for an external program (in this case - php), however, the command's output is still printed to the screen.
How do I prevent it from doing so? My code is:

use strict; use warnings;
print 'Return code:', system('php -l wrong.php'), "\n";

This code does print the return code, but it also print the the output of the command executed.

Any help will be appreciated!

EDIT: further testing shown that this happens while only using the php lint command.. using it with other commands doesnt print anything...

Upvotes: 2

Views: 723

Answers (2)

ennuikiller
ennuikiller

Reputation: 46985

What you want is IPC::Open3:

    use IPC::Open3;
    use Symbol qw(gensym);

    my $err = gensym; #work around IPC::Open3's interface
    my $pid = open3 my $wtr, my $rdr, $err,
            'some cmd', 'arg1', 'arg2', ...;

The separates out stdin into $wtr, stdout into $rdr, and stderr into $err This also gives you maximum control over communicating with the process

Upvotes: 4

brabster
brabster

Reputation: 43600

If you are on a UNIX-like OS, you should be able to redirect the output in the command:

Try: system('php -l wrong.php >> /dev/null') to get rid of what's being sent to stdout.

You could also open the command with a pipe to handle the output directly, which should be more portable.

Upvotes: 2

Related Questions