Skip to content

Commit

Permalink
Don't treat locally defined subs as importable symbols
Browse files Browse the repository at this point in the history
Otherwise we have the issue where:

package Round;
use parent 'Exporter';

use strict;
use warnings;

use Math::Round qw(nearest);
our @EXPORT_OK = qw(round);

sub round {
    my ( $number, $places ) = @_;
    return nearest( 10**-$places, $number );
}

1;

gets an import changed to:

use Math::Round qw( nearest round );
  • Loading branch information
oalders committed Jun 29, 2024
1 parent 6fb98fa commit d1b155a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Revision history for App-perlimports

{{$NEXT}}
- Don't treat locally defined subs as importable symbols (GH#110) (Olaf
Alders). Reported by Glenn Rice.

0.000053 2024-01-13 20:12:59Z
- Don't log a non-JSON message to STDERR on linting success when --json is
Expand Down
3 changes: 2 additions & 1 deletion lib/App/perlimports/Include.pm
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ sub _build_imports {
$self->_document->my_own_inspector->explicit_export_names
)
) {
if ( $self->_is_importable($symbol) ) {
if ( $self->_is_importable($symbol)
&& !$self->_document->is_sub_name("$symbol") ) {
$found{$symbol} = 1;
}
}
Expand Down
38 changes: 38 additions & 0 deletions t/locally-defined-sub.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use strict;
use warnings;

use lib 't/lib', 'test-data/lib';

use Test::Differences qw( eq_or_diff );
use TestHelper qw( doc );
use Test::More import => [qw( done_testing )];

my ( $doc, $log ) = doc(
filename => 'test-data/lib/Local/Round.pm', preserve_unused => 0,
);

my $expected = <<'END';
package Local::Round;
use parent 'Exporter';
use strict;
use warnings;
use Math::Round qw( nearest );
our @EXPORT_OK = qw(round);
sub round {
my ( $number, $places ) = @_;
return nearest( 10**-$places, $number );
}
1;
END

eq_or_diff(
$doc->tidied_document,
$expected,
'locally defined sub is not ignored'
);

done_testing();
15 changes: 15 additions & 0 deletions test-data/lib/Local/Round.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Local::Round;
use parent 'Exporter';

use strict;
use warnings;

use Math::Round qw( nearest );
our @EXPORT_OK = qw(round);

sub round {
my ( $number, $places ) = @_;
return nearest( 10**-$places, $number );
}

1;
1 change: 1 addition & 0 deletions test-needs-cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on 'test' => sub {
requires 'Lingua::EN::Inflect';
requires 'List::AllUtils' => 0;
requires 'LWP::UserAgent' => '6.49';
requires 'Math::Round' => '0.08';
requires 'MetaCPAN::Moose' => 0;
requires 'Mojo' => 0 if "$]" >= 5.016000;
requires 'Moose' => 0;
Expand Down

0 comments on commit d1b155a

Please sign in to comment.