Skip to content

Commit bca5527

Browse files
author
Father Chrysostomos
committed
[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.
1 parent 10030f4 commit bca5527

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,6 +3163,7 @@ dist/base/t/fields.t See if fields work
31633163
dist/base/t/fields-5_6_0.t See if fields work
31643164
dist/base/t/fields-5_8_0.t See if fields work
31653165
dist/base/t/fields-base.t See if fields work
3166+
dist/base/t/incdoc.t Test how base.pm handles '.' in @INC
31663167
dist/base/t/isa.t See if base's behaviour doesn't change
31673168
dist/base/t/lib/Broken.pm Test module for base.pm
31683169
dist/base/t/lib/Dummy.pm Test module for base.pm

dist/base/lib/base.pm

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ sub import {
9797
{
9898
local $SIG{__DIE__};
9999
my $fn = _module_to_filename($base);
100+
my $localinc = $INC[-1] eq '.';
101+
local @INC = @INC[0..$#INC-1] if $localinc;
100102
eval {
101-
local @INC = @INC;
102-
pop @INC if $INC[-1] eq '.';
103103
require $fn
104104
};
105105
# Only ignore "Can't locate" errors from our eval require.
@@ -115,11 +115,19 @@ sub import {
115115
unless (%{"$base\::"}) {
116116
require Carp;
117117
local $" = " ";
118-
Carp::croak(<<ERROR);
118+
my $e = <<ERROR;
119119
Base class package "$base" is empty.
120120
(Perhaps you need to 'use' the module which defines that package first,
121121
or make that module available in \@INC (\@INC contains: @INC).
122122
ERROR
123+
if ($localinc && -e $fn) {
124+
$e .= <<ERROS;
125+
If you mean to load $fn from the current directory, you may
126+
want to try "use lib '.'".
127+
ERROS
128+
}
129+
$e =~ s/\n\z/)\n/;
130+
Carp::croak($e);
123131
}
124132
$sigdie = $SIG{__DIE__} || undef;
125133
}

dist/base/t/incdot.t

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/perl -w
2+
3+
use strict;
4+
5+
use base ();
6+
7+
use Test::More tests => 2;
8+
9+
if ($INC[-1] ne '.') { push @INC, '.' }
10+
11+
my $inc = quotemeta "@INC[0..$#INC-1]";
12+
13+
eval { 'base'->import("foo") };
14+
like $@, qr/\@INC contains: $inc\).\)/,
15+
'Error does not list final dot in @INC (or mention use lib)';
16+
eval { 'base'->import('t::lib::Dummy') };
17+
like $@, qr<\@INC contains: $inc\).\n(?x:
18+
) If you mean to load t/lib/Dummy\.pm from the current >,
19+
'special cur dir message for existing files in . that are ignored';

0 commit comments

Comments
 (0)