File Coverage

blib/lib/Module/FeaturesUtil/Get.pm
Criterion Covered Total %
statement 43 66 65.1
branch 11 28 39.2
condition n/a
subroutine 8 8 100.0
pod 5 5 100.0
total 67 107 62.6


line stmt bran cond sub pod time code
1             package Module::FeaturesUtil::Get;
2              
3 2     2   556337 use strict 'subs', 'vars';
  2         4  
  2         110  
4 2     2   18 use warnings;
  2         5  
  2         172  
5              
6 2     2   21 use Exporter 'import';
  2         5  
  2         2045  
7              
8             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9             our $DATE = '2023-11-11'; # DATE
10             our $DIST = 'Module-FeaturesUtil-Get'; # DIST
11             our $VERSION = '0.006'; # VERSION
12              
13             our @EXPORT_OK = qw(
14             get_feature_set_spec
15             get_features_decl
16             get_feature_val
17             get_feature_defhash
18             module_declares_feature
19             );
20              
21             sub get_feature_set_spec {
22 1     1 1 396071 my ($fsetname, $load, $fatal_on_load_failure) = @_;
23              
24 1         3 my $mod = "Module::Features::$fsetname";
25 1 50       4 if ($load) {
26 0         0 (my $modpm = "$mod.pm") =~ s!::!/!g;
27 0         0 eval { require $modpm; 1 };
  0         0  
  0         0  
28 0 0       0 if ($@) {
29 0 0       0 if ($fatal_on_load_failure) {
30 0         0 die $@;
31             } else {
32 0         0 return {};
33             }
34             }
35             }
36 1         3 return \%{"$mod\::FEATURES_DEF"};
  1         7  
37             }
38              
39             sub get_features_decl {
40 12     12 1 4915 my ($mod, $load, $fatal_on_load_failure) = @_;
41              
42 12         20 my $features_decl;
43              
44             # first, try to get features declaration from MODNAME::_ModuleFeatures's %FEATURES
45             {
46 12         29 my $proxymod = "$mod\::_ModuleFeatures";
47 12         79 (my $proxymodpm = "$proxymod.pm") =~ s!::!/!g;
48 12 50       38 if ($load) {
49 0         0 eval { require $proxymodpm; 1 };
  0         0  
  0         0  
50 0 0       0 last if $@;
51             }
52 12         50 $features_decl = { %{"$proxymod\::FEATURES"} };
  12         72  
53 12 50       44 if (scalar keys %$features_decl) {
54 0         0 $features_decl->{"x.source"} = "pm:$proxymod";
55 0         0 return $features_decl;
56             }
57             }
58              
59             # second, try to get features declaration from MODNAME %FEATURES
60             {
61 12 50       27 if ($load) {
  12         23  
  12         25  
62 0         0 (my $modpm = "$mod.pm") =~ s!::!/!g;
63 0         0 eval { require $modpm; 1 };
  0         0  
  0         0  
64 0 0       0 if ($@) {
65 0 0       0 if ($fatal_on_load_failure) {
66 0         0 die $@;
67             } else {
68 0         0 return {};
69             }
70             }
71             }
72 12         23 $features_decl = { %{"$mod\::FEATURES"} };
  12         55  
73 12         38 $features_decl->{"x.source"} = "pm:$mod";
74 12         49 return $features_decl;
75             }
76              
77 0         0 {};
78              
79             # XXX compare the two if both declarations exist
80             }
81              
82             sub get_feature_val {
83 5     5 1 5780 my ($module_name, $feature_set_name, $feature_name) = @_;
84              
85 5         13 my $features_decl = get_features_decl($module_name);
86 5 100       28 return undef unless $features_decl->{features}{$feature_set_name}; ## no critic: Subroutines::ProhibitExplicitReturnUndef
87              
88 3         8 my $val0 = $features_decl->{features}{$feature_set_name}{$feature_name};
89 3 50       24 return ref $val0 eq 'HASH' ? $val0->{value} : $val0;
90             }
91              
92             sub get_feature_defhash {
93 1     1 1 5229 my ($module_name, $feature_set_name, $feature_name) = @_;
94              
95 1         4 my $features_decl = get_features_decl($module_name);
96 1 50       6 return undef unless $features_decl->{features}{$feature_set_name}; ## no critic: Subroutines::ProhibitExplicitReturnUndef
97              
98 1         12 my $hash0 = $features_decl->{features}{$feature_set_name}{$feature_name};
99 1 50       12 return ref $hash0 eq 'HASH' ? $hash0 : {value=>$hash0};
100             }
101              
102             sub module_declares_feature {
103 5     5 1 4129 my ($module_name, $feature_set_name, $feature_name) = @_;
104              
105 5         14 my $features_decl = get_features_decl($module_name);
106 5 100       28 return undef unless $features_decl->{features}{$feature_set_name}; ## no critic: Subroutines::ProhibitExplicitReturnUndef
107              
108 3         23 exists $features_decl->{features}{$feature_set_name}{$feature_name};
109             }
110              
111             1;
112             # ABSTRACT: Get a feature from a module (following Module::Features specification)
113              
114             __END__