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.


In reply to Re: Modules for catching the output of UNIX/Linux commands by almut
in thread Modules for catching the output of UNIX/Linux commands by onkar_pm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.