Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b685c4db6f
Fetching contributors…

Cannot retrieve contributors at this time

189 lines (139 sloc) 4.945 kb
package Dist::Zilla::Plugin::AutoPrereqs;
use Moose;
'Dist::Zilla::Role::FileFinderUser' => {
default_finders => [ ':InstallModules', ':ExecFiles' ],
'Dist::Zilla::Role::FileFinderUser' => {
method => 'found_test_files',
finder_arg_names => [ 'test_finder' ],
default_finders => [ ':TestFiles' ],
'Dist::Zilla::Role::FileFinderUser' => {
method => 'found_configure_files',
finder_arg_names => [ 'configure_finder' ],
default_finders => [],
=attr finder
This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder>
whose files will be scanned to determine runtime prerequisites. It
may be specified multiple times. The default value is
C<:InstallModules> and C<:ExecFiles>.
=attr test_finder
Just like C<finder>, but for test-phase prerequisites. The default
value is C<:TestFiles>.
=attr configure_finder
Just like C<finder>, but for configure-phase prerequisites. There is
no default value; AutoPrereqs will not determine configure-phase
prerequisites unless you set configure_finder.
use namespace::autoclean;
# ABSTRACT: automatically extract prereqs from your modules
use Moose::Autobox;
use Perl::PrereqScanner 1.005; # do not prune common libs
use PPI;
use CPAN::Meta::Requirements;
use version;
In your F<dist.ini>:
skip = ^Foo|Bar$
skip = ^Other::Dist
This plugin will extract loosely your distribution prerequisites from
your files using L<Perl::PrereqScanner>.
If some prereqs are not found, you can still add them manually with the
L<Prereqs|Dist::Zilla::Plugin::Prereqs> plugin.
This plugin will skip the modules shipped within your dist.
=attr extra_scanners
This is an arrayref of scanner names (as expected by Perl::PrereqScanner).
It will be passed as the C<extra_scanners> parameter to Perl::PrereqScanner.
=attr scanners
This is an arrayref of scanner names (as expected by Perl::PrereqScanner).
If present, it will be passed as the C<scanners> parameter to
Perl::PrereqScanner, which means that it will replace the default list
of scanners.
=attr skips
This is an arrayref of regular expressions, derived from all the 'skip' lines
in the configuration. Any module names matching any of these regexes will not
be registered as prerequisites.
=head1 SEE ALSO
L<Prereqs|Dist::Zilla::Plugin::Prereqs>, L<Perl::PrereqScanner>.
=head1 CREDITS
This plugin was originally contributed by Jerome Quelin.
sub mvp_multivalue_args { qw(extra_scanners scanners skips) }
sub mvp_aliases { return { extra_scanner => 'extra_scanners',
scanner => 'scanners', skip => 'skips' } }
has extra_scanners => (
is => 'ro',
isa => 'ArrayRef[Str]',
default => sub { [] },
has scanners => (
is => 'ro',
isa => 'ArrayRef[Str]',
predicate => 'has_scanners',
has skips => (
is => 'ro',
isa => 'ArrayRef[Str]',
sub register_prereqs {
my $self = shift;
my @modules;
my $scanner = Perl::PrereqScanner->new(
($self->has_scanners ? (scanners => $self->scanners) : ()),
extra_scanners => $self->extra_scanners,
my @sets = (
[ configure => 'found_configure_files' ], # must come before runtime
[ runtime => 'found_files' ],
[ test => 'found_test_files' ],
my %runtime_final;
for my $fileset (@sets) {
my ($phase, $method) = @$fileset;
my $req = CPAN::Meta::Requirements->new;
my $files = $self->$method;
foreach my $file (@$files) {
# parse only perl files
next unless $file->name =~ /\.(?:pm|pl|t|psgi)$/i
|| $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
# RT#76305 skip extra tests produced by ExtraTests plugin
next if $file->name =~ m{^t/(?:author|release)-[^/]*\.t$};
# store module name, to trim it from require list later on
my $module = $file->name;
$module =~ s{^(?:t/)?lib/}{};
$module =~ s{\.pm$}{};
$module =~ s{/}{::}g;
push @modules, $module;
# parse a file, and merge with existing prereqs
my $file_req = $scanner->scan_string($file->content);
# remove prereqs shipped with current dist
$req->clear_requirement($_) for @modules;
# remove prereqs from skiplist
for my $skip (($self->skips || [])->flatten) {
my $re = qr/$skip/;
foreach my $k ($req->required_modules) {
$req->clear_requirement($k) if $k =~ $re;
# we're done, return what we've found
my %got = %{ $req->as_string_hash };
if ($phase eq 'runtime') {
%runtime_final = %got;
} else {
delete $got{$_} for
grep { exists $got{$_} and $runtime_final{$_} ge $got{$_} }
keys %runtime_final;
$self->zilla->register_prereqs({ phase => $phase }, %got);
Jump to Line
Something went wrong with that request. Please try again.