Perl::PrereqScanner - a tool to scan your Perl code for its prerequisites


Perl-PrereqScanner documentation Contained in the Perl-PrereqScanner distribution.

Index


Code Index:

NAME

Top

Perl::PrereqScanner - a tool to scan your Perl code for its prerequisites

VERSION

Top

version 1.004

SYNOPSIS

Top

  use Perl::PrereqScanner;
  my $scanner = Perl::PrereqScanner->new;
  my $prereqs = $scanner->scan_ppi_document( $ppi_doc );
  my $prereqs = $scanner->scan_file( $file_path );
  my $prereqs = $scanner->scan_string( $perl_code );

DESCRIPTION

Top

The scanner will extract loosely your distribution prerequisites from your files.

The extraction may not be perfect but tries to do its best. It will currently find the following prereqs:

It will trim the following pragamata: strict, warnings, and lib. base is trimmed unless a specific version is required. parent is kept, since it's only recently become a core library.

Scanner Plugins

Perl::PrereqScanner works by running a series of scanners over a PPI::Document representing the code to scan. By default the "Perl5" and "Moose" scanners are run. You can supply your own scanners when constructing your PrereqScanner:

  # Do not use the Moose scanner, only Perl5:
  my $scanner = Perl::PrereqScanner->new({ plugins => [ qw(Perl5) ] });

  # Use any stock plugins, plus Example:
  my $scanner = Perl::PrereqScanner->new({ extra_plugins => [ qw(Example) ] });

METHODS

Top

scan_string

  my $prereqs = $scanner->scan_string( $perl_code );

Given a string containing Perl source code, this method returns a Version::Requirements object describing the modules it requires.

This method will throw an exception if PPI fails to parse the code.

scan_file

  my $prereqs = $scanner->scan_file( $path );

Given a file path to a Perl document, this method returns a Version::Requirements object describing the modules it requires.

This method will throw an exception if PPI fails to parse the code.

scan_ppi_document

  my $prereqs = $scanner->scan_ppi_document( $ppi_doc );

Given a PPI::Document, this method returns a Version::Requirements object describing the modules it requires.

AUTHORS

Top

COPYRIGHT AND LICENSE

Top


Perl-PrereqScanner documentation Contained in the Perl-PrereqScanner distribution.

use 5.008;
use strict;
use warnings;

package Perl::PrereqScanner;
BEGIN {
  $Perl::PrereqScanner::VERSION = '1.004';
}
use Moose;
# ABSTRACT: a tool to scan your Perl code for its prerequisites

use List::Util qw(max);
use Params::Util qw(_CLASS);
use Perl::PrereqScanner::Scanner;
use PPI 1.205; # module_version
use String::RewritePrefix 0.005 rewrite => {
  -as => '__rewrite_scanner',
  prefixes => { '' => 'Perl::PrereqScanner::Scanner::', '=' => '' },
};

use Version::Requirements 0.100630; # merge with 0-min bug fixed

use namespace::autoclean;

has scanners => (
  is  => 'ro',
  isa => 'ArrayRef[Perl::PrereqScanner::Scanner]',
  init_arg => undef,
  writer   => '_set_scanners',
);

sub __scanner_from_str {
  my $class = __rewrite_scanner($_[0]);
  confess "illegal class name: $class" unless _CLASS($class);
  eval "require $class; 1" or die $@;
  return $class->new;
}

sub __prepare_scanners {
  my ($self, $specs) = @_;
  my @scanners = map {; ref $_ ? $_ : __scanner_from_str($_) } @$specs;

  return \@scanners;
}

sub BUILD {
  my ($self, $arg) = @_;

  my @scanners = @{ $arg->{scanners} || [ qw(Perl5 TestMore Moose) ] };
  my @extra_scanners = @{ $arg->{extra_scanners} || [] };

  my $scanners = $self->__prepare_scanners([ @scanners, @extra_scanners ]);

  $self->_set_scanners($scanners);
}


sub scan_string {
  my ($self, $str) = @_;
  my $ppi = PPI::Document->new( \$str );
  confess "PPI parse failed" unless defined $ppi;

  return $self->scan_ppi_document( $ppi );
}



sub scan_file {
  my ($self, $path) = @_;
  my $ppi = PPI::Document->new( $path );
  confess "PPI failed to parse '$path'" unless defined $ppi;

  return $self->scan_ppi_document( $ppi );
}



sub scan_ppi_document {
  my ($self, $ppi_doc) = @_;

  my $req = Version::Requirements->new;

  for my $scanner (@{ $self->{scanners} }) {
    $scanner->scan_for_prereqs($ppi_doc, $req);
  }

  return $req;
}

1;



__END__