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 975ce9f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions 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 All @@ -350,7 +351,8 @@ sub _build_imports {
# Sub::Exporter
if ( $self->_imports_remain( \%found ) ) {
for my $func ( $self->_document->sub_exporter_export_list ) {
if ( $self->_is_importable($func) ) {
if ( $self->_is_importable($func)
&& !$self->_document->is_sub_name("$func") ) {
$found{$func}++;
}
}
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 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;

0 comments on commit 975ce9f

Please sign in to comment.