croak "req_ok_for() expects a requirement group name"
unless $group;
- $class->_check_deps ($group) unless $req_availability_cache{$group};
-
- return $req_availability_cache{$group}{status};
+ return $class->_check_deps($group)->{status};
}
sub req_missing_for {
croak "req_missing_for() expects a requirement group name"
unless $group;
- $class->_check_deps ($group) unless $req_availability_cache{$group};
-
- return $req_availability_cache{$group}{missing};
+ return $class->_check_deps($group)->{missing};
}
sub req_errorlist_for {
croak "req_errorlist_for() expects a requirement group name"
unless $group;
- $class->_check_deps ($group) unless $req_availability_cache{$group};
-
- return $req_availability_cache{$group}{errorlist};
+ return $class->_check_deps($group)->{errorlist};
}
sub _check_deps {
my ($class, $group) = @_;
- my $deps = $class->req_list_for ($group);
+ return $req_availability_cache{$group} ||= do {
+
+ my $deps = $class->req_list_for ($group);
+
+ my %errors;
+ for my $mod (keys %$deps) {
+ my $req_line = "require $mod;";
+ if (my $ver = $deps->{$mod}) {
+ $req_line .= "$mod->VERSION($ver);";
+ }
+
+ eval $req_line;
+
+ $errors{$mod} = $@ if $@;
+ }
+
+ my $res;
- my %errors;
- for my $mod (keys %$deps) {
- if (my $ver = $deps->{$mod}) {
- eval "use $mod $ver ()";
+ if (keys %errors) {
+ my $missing = join (', ', map { $deps->{$_} ? "$_ >= $deps->{$_}" : $_ } (sort keys %errors) );
+ $missing .= " (see $class for details)" if $reqs->{$group}{pod};
+ $res = {
+ status => 0,
+ errorlist => \%errors,
+ missing => $missing,
+ };
}
else {
- eval "require $mod";
+ $res = {
+ status => 1,
+ errorlist => {},
+ missing => '',
+ };
}
- $errors{$mod} = $@ if $@;
- }
-
- if (keys %errors) {
- my $missing = join (', ', map { $deps->{$_} ? "$_ >= $deps->{$_}" : $_ } (sort keys %errors) );
- $missing .= " (see $class for details)" if $reqs->{$group}{pod};
- $req_availability_cache{$group} = {
- status => 0,
- errorlist => { %errors },
- missing => $missing,
- };
- }
- else {
- $req_availability_cache{$group} = {
- status => 1,
- errorlist => {},
- missing => '',
- };
- }
+ $res;
+ };
}
sub req_group_list {
or die "Hrmm? No sqlt dep?";
my @chunks = (
- <<"EOC",
+ <<'EOC',
#########################################################################
##################### A U T O G E N E R A T E D ########################
#########################################################################
'=head1 NAME',
"$class - Optional module dependency specifications (for module authors)",
'=head1 SYNOPSIS',
- <<EOS,
+ <<"EOS",
Somewhere in your build-file (e.g. L<Module::Install>'s Makefile.PL):
...
'=head1 METHODS',
'=head2 req_group_list',
'=over',
- '=item Arguments: $none',
+ '=item Arguments: none',
'=item Returns: \%list_of_requirement_groups',
'=back',
- <<EOD,
+ <<'EOD',
This method should be used by DBIx::Class packagers, to get a hashref of all
dependencies keyed by dependency group. Each key (group name) can be supplied
to one of the group-specific methods below.
'=item Arguments: $group_name',
'=item Returns: \%list_of_module_version_pairs',
'=back',
- <<EOD,
+ <<'EOD',
This method should be used by DBIx::Class extension authors, to determine the
version of modules a specific feature requires in the B<current> version of
DBIx::Class. See the L</SYNOPSIS> for a real-world
'=item Arguments: $group_name',
'=item Returns: 1|0',
'=back',
- 'Returns true or false depending on whether all modules required by C<$group_name> are present on the system and loadable',
+ <<'EOD',
+Returns true or false depending on whether all modules required by
+C<$group_name> are present on the system and loadable.
+EOD
'=head2 req_missing_for',
'=over',
'=item Arguments: $group_name',
'=item Returns: $error_message_string',
'=back',
- <<EOD,
+ <<"EOD",
Returns a single line string suitable for inclusion in larger error messages.
This method would normally be used by DBIx::Class core-module author, to
indicate to the user that he needs to install specific modules before he will
use Test::More;
use lib qw(t/lib);
use Scalar::Util; # load before we break require()
-use Carp (); # Carp is not used in the test, but we want to have it loaded for proper %INC comparison
+use Carp (); # Carp is not used in the test, but we want to have it loaded for proper %INC comparison
# a dummy test which lazy-loads more modules (so we can compare INC below)
ok (1);
);
# make module loading impossible, regardless of actual libpath contents
-@INC = (sub { die('Optional Dep Test') } );
-
-ok (
- ! DBIx::Class::Optional::Dependencies->req_ok_for ('deploy'),
- 'deploy() deps missing',
-);
-
-like (
- DBIx::Class::Optional::Dependencies->req_missing_for ('deploy'),
- qr/^SQL::Translator \>\= \d/,
- 'expected missing string contents',
-);
-
-like (
- DBIx::Class::Optional::Dependencies->req_errorlist_for ('deploy')->{'SQL::Translator'},
- qr/Optional Dep Test/,
- 'custom exception found in errorlist',
-);
-
+{
+ local @INC = (sub { die('Optional Dep Test') } );
+
+ ok (
+ ! DBIx::Class::Optional::Dependencies->req_ok_for ('deploy'),
+ 'deploy() deps missing',
+ );
+
+ like (
+ DBIx::Class::Optional::Dependencies->req_missing_for ('deploy'),
+ qr/^SQL::Translator \>\= \d/,
+ 'expected missing string contents',
+ );
+
+ like (
+ DBIx::Class::Optional::Dependencies->req_errorlist_for ('deploy')->{'SQL::Translator'},
+ qr/Optional Dep Test/,
+ 'custom exception found in errorlist',
+ );
+}
#make it so module appears loaded
$INC{'SQL/Translator.pm'} = 1;
'expected empty errorlist',
);
-
done_testing;