Skip to content

Commit dafa511

Browse files
committed
check for existence of sub naming subs before using them
Sub::Util ships with new versions of List::Util, but its subs are provided by the XS component of List::Util. All Sub::Name itself does is load List::Util to ensure the subs are available. If List::Util is downgraded or the new version is otherwise unavailable, Sub::Util will load fine but the subs won't be installed. We need to make sure the actual subs exist before we try to use them.
1 parent aebf7dc commit dafa511

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/Moo/_Utils.pm

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ sub _getglob { \*{$_[0]} }
66
sub _getstash { \%{"$_[0]::"} }
77

88
use constant lt_5_8_3 => ( $] < 5.008003 or $ENV{MOO_TEST_PRE_583} ) ? 1 : 0;
9-
use constant can_haz_subutil => $INC{"Sub/Util.pm"}
10-
|| ( !$INC{"Sub/Name.pm"} && eval { require Sub::Util } );
11-
use constant can_haz_subname => $INC{"Sub/Name.pm"}
12-
|| ( !$INC{"Sub/Util.pm"} && eval { require Sub::Name } );
9+
use constant can_haz_subutil => (
10+
$INC{"Sub/Util.pm"}
11+
|| ( !$INC{"Sub/Name.pm"} && eval { require Sub::Util } )
12+
) && defined &Sub::Util::set_subname;
13+
use constant can_haz_subname => (
14+
$INC{"Sub/Name.pm"}
15+
|| ( !$INC{"Sub/Util.pm"} && eval { require Sub::Name } )
16+
) && defined &Sub::Name::subname;
1317

1418
use Moo::_strictures;
1519
use Module::Runtime qw(use_package_optimistically module_notional_filename);

t/moo-utils-_name_coderef.t

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ use Moo::_strictures;
22
use Test::More;
33

44
BEGIN {
5+
no warnings 'redefine';
56
$INC{'Sub/Name.pm'} = 1;
7+
defined &Sub::Name::subname or *Sub::Name::subname = sub {};
68
$INC{'Sub/Util.pm'} = 1;
9+
defined &Sub::Util::set_subname or *Sub::Util::set_subname = sub {};
710
}
811

912
use Moo::_Utils;

0 commit comments

Comments
 (0)