How to capture a return value of a Perl code run by another Perl code on the command line?

I have a Perl code which returns 0 or anything else.

I call this code withing another Perl code for different $file inputs as shown below. My concern is the way I run this command using the system call.

Is this system command going to return the return value of "check" file.

If not, how can I run the "check" code and capture its output value in the below code?

foreach my $file (@selected_files){
		my $cmd = '/some/path/to/a/directory/check -f $file';
		my $out = system ($cmd);
		push(@result, $out);

	if (any { $_ != 0 } @result) { 
		return -1;
		return 0;

Open in new window

Who is Participating?
The most common approach is to use ` ` backticks or the qx() operator instead of the system function.

my $out = `$cmd`;


my $out = qx( $cmd );

If you need/want more flexibility and control, then use IPC::Open2 or IPC::Open3
Your wording is a little vague.  Do you want to capture exit code or the literal output that check sent to stdout?

The system function returns the commands exit code, not the commands stdout output.
TolgarAuthor Commented:
@FishMonger: You are right. I'd like to capture the literal output that check sent to stdout.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.