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

Posted on 2012-09-06
Last Modified: 2012-09-06
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

Question by:Tolgar
    LVL 28

    Expert Comment

    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.

    Author Comment

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

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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…
    This video discusses moving either the default database or any database to a new volume.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now