summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2016-08-10 08:16:49 +0100
committerSteve Hay <steve.m.hay@googlemail.com>2016-08-10 08:30:57 +0100
commit37e3ca14ffd858d3892118cd76f2e1e80c767d64 (patch)
treefc1ff27653100eee9a0a56c946c3307ea22357ef
parent04f7594e04e0aa46d8b168978ae07a080cb49b47 (diff)
downloadperl-37e3ca14ffd858d3892118cd76f2e1e80c767d64.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. (cherry picked from commit bca552795994a553e07b38a6f82a233533919926)
-rw-r--r--MANIFEST1
-rw-r--r--dist/base/lib/base.pm14
-rw-r--r--dist/base/t/incdot.t19
3 files changed, 31 insertions, 3 deletions
diff --git a/MANIFEST b/MANIFEST
index a8d68546f8..48b739424e 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -2892,6 +2892,7 @@ dist/base/t/fields-5_6_0.t See if fields work
dist/base/t/fields-5_8_0.t See if fields work
dist/base/t/fields-base.t See if fields work
dist/base/t/fields.t See if fields work
+dist/base/t/incdoc.t Test how base.pm handles '.' in @INC
dist/base/t/isa.t See if base's behaviour doesn't change
dist/base/t/lib/Broken.pm Test module for base.pm
dist/base/t/lib/Dummy.pm Test module for base.pm
diff --git a/dist/base/lib/base.pm b/dist/base/lib/base.pm
index ff1a16c1f7..8dc62bbd1c 100644
--- a/dist/base/lib/base.pm
+++ b/dist/base/lib/base.pm
@@ -96,9 +96,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.
@@ -114,11 +114,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';