Over a million developers have joined DZone.

Parse Solaris Pkginfo -l In Perl

·
// description of your code here


#!/usr/bin/perl -w
#
#
#
use strict;
use Data::Dumper;

my $pkg_list = "/usr/bin/pkginfo -l|";

open (PKG_LIST, $pkg_list) || die "Can't run '$pkg_list': $!\n";
my $pkg;
while (defined (my $line = )) {

    chomp $line;

    my $match = ( $line =~ /PKGINST:/ .. $line =~ /^$/ );

    if ( $match && $match !~ /E0/ ) {

        if ( $line =~ /^\s+([A-Z]+):\s+(.*)$/ ) {
            my ($key, $val) = ($1, $2);
            if ( $key eq 'FILES' ) {
                if ( $val =~ /^(\d+) (.*)$/ ) {
                    $pkg->{FILES}->{$2} = $1;
                }
            }
            else {
                $pkg->{$1} = $2;
            }
        }
        elsif ( $line =~ /^\s+([0-9]+) (.*)$/ ) {
            $pkg->{FILES}->{$2} = $1;
        }
        else {
            print "Unrecognized output: [$line]\n";
        }

    }
    else {

        #
        # Blank line between packages
        #
        print Dumper $pkg;

    }
}
close PKG_LIST;
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}