diff options
author | Father Chrysostomos <sprout@cpan.org> | 2016-07-31 13:51:36 -0700 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2016-07-31 13:53:05 -0700 |
commit | bca552795994a553e07b38a6f82a233533919926 (patch) | |
tree | e2c51131490a4c22ccd88f5802a4807773033160 /dist/base | |
parent | 10030f4b9e57e68d03a6b1eb1a9bed9fb389e8ac (diff) | |
download | perl-bca552795994a553e07b38a6f82a233533919926.tar.gz |
[perl #128769] Improve base.pm @INC '.' handling
• Localise @INC only if necessary.
• Don’t mention '.' in the @INC list in the error message, since it
was not in the @INC that was searched (this is accomplished by local-
ising @INC in the same scope as the error).
• If a file exists that would have been loaded had '.' not been
ignored, mention it and suggest ‘use lib’.
• Use the same number of closing as opening parentheses in the
error message.
Diffstat (limited to 'dist/base')
-rw-r--r-- | dist/base/lib/base.pm | 14 | ||||
-rw-r--r-- | dist/base/t/incdot.t | 19 |
2 files changed, 30 insertions, 3 deletions
diff --git a/dist/base/lib/base.pm b/dist/base/lib/base.pm index cfe53aea99..1093eb6f4f 100644 --- a/dist/base/lib/base.pm +++ b/dist/base/lib/base.pm @@ -97,9 +97,9 @@ sub import { { local $SIG{__DIE__}; my $fn = _module_to_filename($base); + my $localinc = $INC[-1] eq '.'; + local @INC = @INC[0..$#INC-1] if $localinc; eval { - local @INC = @INC; - pop @INC if $INC[-1] eq '.'; require $fn }; # Only ignore "Can't locate" errors from our eval require. @@ -115,11 +115,19 @@ sub import { unless (%{"$base\::"}) { require Carp; local $" = " "; - Carp::croak(<<ERROR); + my $e = <<ERROR; Base class package "$base" is empty. (Perhaps you need to 'use' the module which defines that package first, or make that module available in \@INC (\@INC contains: @INC). ERROR + if ($localinc && -e $fn) { + $e .= <<ERROS; + If you mean to load $fn from the current directory, you may + want to try "use lib '.'". +ERROS + } + $e =~ s/\n\z/)\n/; + Carp::croak($e); } $sigdie = $SIG{__DIE__} || undef; } diff --git a/dist/base/t/incdot.t b/dist/base/t/incdot.t new file mode 100644 index 0000000000..fadebc4a45 --- /dev/null +++ b/dist/base/t/incdot.t @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w + +use strict; + +use base (); + +use Test::More tests => 2; + +if ($INC[-1] ne '.') { push @INC, '.' } + +my $inc = quotemeta "@INC[0..$#INC-1]"; + +eval { 'base'->import("foo") }; +like $@, qr/\@INC contains: $inc\).\)/, + 'Error does not list final dot in @INC (or mention use lib)'; +eval { 'base'->import('t::lib::Dummy') }; +like $@, qr<\@INC contains: $inc\).\n(?x: + ) If you mean to load t/lib/Dummy\.pm from the current >, + 'special cur dir message for existing files in . that are ignored'; |