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

Hi,
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;
	}
	else{
		return 0;
	}

Open in new window

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

my $out = `$cmd`;

or

my $out = qx( $cmd );

If you need/want more flexibility and control, then use IPC::Open2 or IPC::Open3
0
 
FishMongerCommented:
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.
0
 
TolgarAuthor Commented:
@FishMonger: You are right. I'd like to capture the literal output that check sent to stdout.
0
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.