File Coverage

blib/lib/Module/FeaturesUtil/Get.pm
Criterion Covered Total %
statement 38 55 69.0
branch 9 20 45.0
condition n/a
subroutine 7 7 100.0
pod 4 4 100.0
total 58 86 67.4


line stmt bran cond sub pod time code
1             package Module::FeaturesUtil::Get;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2021-02-25'; # DATE
5             our $DIST = 'Module-FeaturesUtil-Get'; # DIST
6             our $VERSION = '0.003'; # VERSION
7              
8 1     1   77048 use strict 'subs', 'vars';
  1         22  
  1         38  
9 1     1   6 use warnings;
  1         2  
  1         43  
10              
11 1     1   6 use Exporter 'import';
  1         2  
  1         632  
12             our @EXPORT_OK = qw(
13             get_feature_set_spec
14             get_features_decl
15             get_feature_val
16             module_declares_feature
17             );
18              
19             sub get_feature_set_spec {
20 1     1 1 1144 my ($fsetname, $load) = @_;
21              
22 1         4 my $mod = "Module::Features::$fsetname";
23 1 50       4 if ($load) {
24 0         0 (my $modpm = "$mod.pm") =~ s!::!/!g;
25 0         0 eval { require $modpm; 1 };
  0         0  
  0         0  
26 0 0       0 last if $@;
27             }
28 1         3 return \%{"$mod\::FEATURES_DEF"};
  1         8  
29             }
30              
31             sub get_features_decl {
32 11     11 1 2598 my ($mod, $load) = @_;
33              
34 11         16 my $features_decl;
35              
36             # first, try to get features declaration from MODNAME::_ModuleFeatures's %FEATURES
37             {
38 11         26 my $proxymod = "$mod\::_ModuleFeatures";
39 11         56 (my $proxymodpm = "$proxymod.pm") =~ s!::!/!g;
40 11 50       33 if ($load) {
41 0         0 eval { require $proxymodpm; 1 };
  0         0  
  0         0  
42 0 0       0 last if $@;
43             }
44 11         18 $features_decl = { %{"$proxymod\::FEATURES"} };
  11         54  
45 11 50       37 if (scalar keys %$features_decl) {
46 0         0 $features_decl->{"x.source"} = "pm:$proxymod";
47 0         0 return $features_decl;
48             }
49             }
50              
51             # second, try to get features declaration from MODNAME %FEATURES
52             {
53 11 50       18 if ($load) {
  11         16  
  11         20  
54 0         0 (my $modpm = "$mod.pm") =~ s!::!/!g;
55 0         0 eval { require $modpm; 1 };
  0         0  
  0         0  
56 0 0       0 last if $@;
57             }
58 11         16 $features_decl = { %{"$mod\::FEATURES"} };
  11         44  
59 11         29 $features_decl->{"x.source"} = "pm:$mod";
60 11         29 return $features_decl;
61             }
62              
63 0         0 {};
64              
65             # XXX compare the two if both declarations exist
66             }
67              
68             sub get_feature_val {
69 5     5 1 2252 my ($module_name, $feature_set_name, $feature_name) = @_;
70              
71 5         11 my $features_decl = get_features_decl($module_name);
72 5 100       33 return undef unless $features_decl->{features}{$feature_set_name};
73              
74 3         7 my $val0 = $features_decl->{features}{$feature_set_name}{$feature_name};
75 3 50       21 return ref $val0 eq 'HASH' ? $val0->{value} : $val0;
76             }
77              
78             sub module_declares_feature {
79 5     5 1 2384 my ($module_name, $feature_set_name, $feature_name) = @_;
80              
81 5         11 my $features_decl = get_features_decl($module_name);
82 5 100       22 return undef unless $features_decl->{features}{$feature_set_name};
83              
84 3         15 exists $features_decl->{features}{$feature_set_name}{$feature_name};
85             }
86              
87             1;
88             # ABSTRACT: Get a feature
89              
90             __END__