Skip to content

Commit

Permalink
[perl #128769] Improve base.pm @inc '.' handling
Browse files Browse the repository at this point in the history
• 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.
  • Loading branch information
Father Chrysostomos committed Jul 31, 2016
1 parent 10030f4 commit bca5527
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -3163,6 +3163,7 @@ dist/base/t/fields.t See if fields work
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/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
Expand Down
14 changes: 11 additions & 3 deletions dist/base/lib/base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down
19 changes: 19 additions & 0 deletions dist/base/t/incdot.t
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit bca5527

Please sign in to comment.