From 04b9b9271d16448eba059f5ddff6a1efbd67c770 Mon Sep 17 00:00:00 2001 From: Rob Halliday Date: Thu, 3 Nov 2016 14:10:01 +0000 Subject: [PATCH] Add $self->{inc} to @INC whilst evaluating version WWW::Scripter defines multiple packages in the same file, some of those packages contain a block of text that Module::Metadata tries to evaluate to get a version. The line that Module::Metadata tries to evaluate is "use WWW::Scripter; use WWW::Scripter; $VERSION = $WWW'Scripter'VERSION;" When using Carton to install WWW::Scripter this causes Module::Metadata to error with "Can't find module". If we add $self->{inc} to @INC before evaluating the version line then Perl looks in the right places for the WWW::Scripter module. --- lib/Module/Metadata.pm | 4 ++++ t/lib/0_1/Bar.pm | 12 ++++++++++++ t/version.t | 10 ++++------ 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 t/lib/0_1/Bar.pm diff --git a/lib/Module/Metadata.pm b/lib/Module/Metadata.pm index 0066535..0de3075 100644 --- a/lib/Module/Metadata.pm +++ b/lib/Module/Metadata.pm @@ -693,6 +693,10 @@ sub _evaluate_version_line { $eval = $1 if $eval =~ m{^(.+)}s; local $^W; + + # if inc has been set, look there for any required modules + local @INC = (@{$self->{inc}}, @INC) if defined $self->{inc}; + # Try to get the $VERSION my $vsub = __clean_eval($eval); # some modules say $VERSION $Foo::Bar::VERSION, but Foo::Bar isn't diff --git a/t/lib/0_1/Bar.pm b/t/lib/0_1/Bar.pm new file mode 100644 index 0000000..bb3303d --- /dev/null +++ b/t/lib/0_1/Bar.pm @@ -0,0 +1,12 @@ +package Bar; +our $VERSION = '0.1'; + +package Bar::History; + +# adapted from WWW::Scripter +<<'mldistwatch' if 0; +use Bar; $VERSION = $Bar'VERSION; +mldistwatch +our $VERSION = $Bar'VERSION; + +1; diff --git a/t/version.t b/t/version.t index f97a19d..99d7208 100644 --- a/t/version.t +++ b/t/version.t @@ -4,7 +4,7 @@ use Test::More; use Module::Metadata; use lib "t/lib/0_2"; -plan tests => 4; +plan tests => 6; require Foo; is($Foo::VERSION, 0.2, 'affirmed version of loaded module'); @@ -17,8 +17,6 @@ is($Foo::VERSION, 0.2, 'loaded module still retains its version'); ok(eval "use Foo 0.2; 1", 'successfully loaded module again') or diag 'got exception: ', $@; - - - - - +my $bar_meta = Module::Metadata->new_from_module("Bar", inc => [ "t/lib/0_1" ] ); +is scalar @{$bar_meta->{packages}}, 2, 'found 2 packages in Bar'; +is $bar_meta->{versions}{'Bar::History'}, $bar_meta->{versions}{'Bar'}, 'Bars version gets passed to Bar::History';