Alien-Packages

 view release on metacpan or  search on metacpan

lib/Alien/Packages.pm  view on Meta::CPAN

package Alien::Packages;

use warnings;
use strict;

require 5.008;
require Module::Pluggable::Object;

=head1 NAME

Alien::Packages - Find information of installed packages

=cut

our $VERSION = '0.003';

=head1 SYNOPSIS

    my $ap = Alien::Packages->new();

    my @packages = $ap->list_packages();
    foreach my $pkg (@packages)
    {
	print "$pkg->[0] version $pkg->[1]: $pkg->[2]\n";
    }

    my %perl_owners = $ap->list_fileowners( File::Spec->rel2abs( $^X ) );
    while( my ($fn, $pkg) = each( %perl_owners ) )
    {
	print "$fn is provided by ", join( ", ", @$pkg ), "\n";
    }

=head1 SUBROUTINES/METHODS

=head2 new

Instantiates new Alien::Packages object. Attributes can be specified
for used finder (of type L<Module::Pluggable::Object>). Additionally,

=over 4

=item C<only_loaded>

Use only plugins which are still loaded.

=back

can be specified with a true value. This forces to grep C<%INC> instead
of using Module::Pluggable.

=cut

sub new
{
    my ( $class, %attrs ) = @_;
    my $self = bless( { plugins => [], }, $class );

    my $only_loaded = delete $attrs{only_loaded};

    if ($only_loaded)
    {
        my @search_path = __PACKAGE__ eq $class ? (__PACKAGE__) : ( __PACKAGE__, $class );
        foreach my $path (@search_path)
        {
            $path =~ s|::|/|g;
            $path .= "/";
            my @loadedModules = grep { 0 == index( $_, $path ) } keys %INC;
            foreach my $module (@loadedModules)
            {
                $module =~ s|/|::|;
                $module =~ s/\.pm$//;
                next unless ( $module->can('usable') && $module->usable() );
                push( @{ $self->{plugins} }, $module->new() );
            }
        }
    }
    else
    {
        %attrs = (
                   require     => 1,
                   search_path => [ __PACKAGE__ eq $class ? __PACKAGE__ : ( __PACKAGE__, $class ) ],
                   inner       => 0,
                   %attrs,
                 );
        my $finder     = Module::Pluggable::Object->new(%attrs);
        my @pkgClasses = $finder->plugins();
        foreach my $pkgClass (@pkgClasses)
        {
            next unless ( $pkgClass->can('usable') && $pkgClass->usable() );
            push( @{ $self->{plugins} }, $pkgClass->new() );
        }
    }

    return $self;
}

=head2 list_packages

Lists the installed packages on the system (if the caller has the
permission to do).

Results in a list of array references, whereby each item contains:

  {
      PkgType => $pkg_type, # e.g. 'dpkg', 'pkgsrc', ...
      Package => $pkg_name,
      Version => $version,
      Summary => $summary,
  }

C<type> is the packager type, e.g. I<rpm>, I<lpp> or I<pkgsrc>.

=cut

sub list_packages
{



( run in 3.241 seconds using v1.01-cache-2.11-cpan-2c0d6866c4f )