Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2012-09-06
3
Medium Priority
?
487 Views
Last Modified: 2012-09-06
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

0
Comment
Question by:Tolgar
  • 2
3 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 38373642
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
 

Author Comment

by:Tolgar
ID: 38373825
@FishMonger: You are right. I'd like to capture the literal output that check sent to stdout.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 2000 total points
ID: 38373875
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question