generic module to catch the output of the common UNIX/Linux commands ... into Perl data structures such as hashes
Others have already made a couple of suggestions, so let me just
focus on the generic aspect of capturing and parsing the output
of arbitrary Unix commands.
To have a generic solution, such a module would have to devise some mini
(or rather maxi) language that allows you to specify how the respective
output is to be parsed and split up into Perl data structures.
This would likely turn out to be so complex that one might as well just
write the few lines of Perl that will achieve the same... For example,
the code to capture and split up the output of ls -l might look
something like this:
#!/usr/bin/perl
use strict;
use warnings;
my $cmd = 'ls -l [0-9]*.pl';
my %fileinfo;
open my $capt, "$cmd |" or die "Couldn't run shell/'$cmd': $!";
while (<$capt>) {
my ($mode, $links, $owner, $group, $size, $date, $time, $fname) =
+split;
my ($type, $perm) = unpack "aa*", $mode;
$fileinfo{$fname} = {
SIZE => $size,
DATE => "$date $time",
OWNER => $owner,
PERM => $perm,
} if $type eq '-';
}
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
print Dumper \%fileinfo;
__END__
$VAR1 = {
...
'828461.pl' => {
'DATE' => '2010-03-13 21:20',
'OWNER' => 'almut',
'PERM' => 'rwxr--r--',
'SIZE' => '502'
},
'828472.pl' => {
'DATE' => '2010-03-13 20:31',
'OWNER' => 'almut',
'PERM' => 'rwxr--r--',
'SIZE' => '83'
},
'828529.pl' => {
'DATE' => '2010-03-14 12:52',
'OWNER' => 'almut',
'PERM' => 'rwxr--r--',
'SIZE' => '807'
}
};
OTOH, as Corion has already hinted at, Perl comes with all kinds
of nifty stuff that often allows you get away without having to invoke
external Unix commands. In other words, the following would achieve
almost the same as the above script (translating '755'
($mode) into 'rwxr-xr-x' etc. is left as an exercise for the reader):
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime); # for date formatting
my %fileinfo;
for my $fname (glob "[0-9]*.pl") {
my ($mode, $uid, $size, $mtime) = (stat $fname)[2,4,7,9];
$fileinfo{$fname} = {
SIZE => $size,
DATE => strftime("%Y-%m-%d %H:%M", localtime($mtime)),
OWNER => scalar getpwuid($uid),
PERM => sprintf("%o", $mode & 07777),
} if -f _;
}
# ...dump %fileinfo
Of course, if you need a solution for a specific Unix command,
say lsof, you might want to search CPAN
first, before beginning to reinvent the wheel.
|