Wednesday, April 13, 2011

How to Capture Shell Command Output in Perl

Formatting Captured Shell Output

Just being picky, this will print all of the output lines on a single line separated by spaces. Since you probably want to see them on separate lines, you could either:

Method 1: Retain Original Newlines (Do Not Chomp)

Perl
my @files = qx(ls);
print @files;

Method 2: Chomp and Append Newlines on Output
Perl
chomp(my @files = qx(ls));
print "$_\n" foreach @files;

No comments: