Getting return value and output text with Perl...

I know using Perl backticks call I can execute an OS command and easily get the output.

I know using Perl system() call I can execute an OS command and easily get the return value.

Today I found out a way to get both.

This example is on Windows, but if you are on Unix, substitute 'ls' for 'dir'.

#!/bin/perl

# Execute dir command
$command = `dir`;

# Show the output of the command
#split by newlines, put into @alloutput array
@alloutput = split /\n/, $command;  

#loop once for each item in @alloutput array
foreach $outputline (@alloutput) {   
    print qq[Output: $outputline\n];
}

# Now show the return value
print "Return value: $?";

Voila! You can have your output and return value too!

-- Dave

Tags: 

Add new comment