From 975ce9f08cbdd50d4f94a425871a7732d2bb1101 Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Sat, 29 Jun 2024 13:38:11 -0400 Subject: [PATCH] Don't treat locally defined subs as importable symbols 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 ); --- lib/App/perlimports/Include.pm | 6 ++++-- t/locally-defined-sub.t | 38 ++++++++++++++++++++++++++++++++++ test-data/lib/Local/Round.pm | 15 ++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 t/locally-defined-sub.t create mode 100644 test-data/lib/Local/Round.pm diff --git a/lib/App/perlimports/Include.pm b/lib/App/perlimports/Include.pm index bb0e935..95c3490 100644 --- a/lib/App/perlimports/Include.pm +++ b/lib/App/perlimports/Include.pm @@ -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; } } @@ -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}++; } } diff --git a/t/locally-defined-sub.t b/t/locally-defined-sub.t new file mode 100644 index 0000000..92dba15 --- /dev/null +++ b/t/locally-defined-sub.t @@ -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(); diff --git a/test-data/lib/Local/Round.pm b/test-data/lib/Local/Round.pm new file mode 100644 index 0000000..298d077 --- /dev/null +++ b/test-data/lib/Local/Round.pm @@ -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;