File Coverage

blib/lib/Modern/Perl/Prelude.pm
Criterion Covered Total %
statement 92 92 100.0
branch 28 28 100.0
condition 6 6 100.0
subroutine 19 19 100.0
pod n/a
total 145 145 100.0


line stmt bran cond sub pod time code
1             package Modern::Perl::Prelude;
2              
3 7     7   1490540 use v5.30;
  7         21  
4 7     7   30 use strict;
  7         13  
  7         140  
5 7     7   23 use warnings;
  7         8  
  7         499  
6              
7             # ABSTRACT: Project prelude for modern Perl style on Perl 5.30+
8             our $VERSION = '0.008';
9              
10 7     7   2617 use Import::Into ();
  7         15467  
  7         126  
11 7     7   48 use strict ();
  7         8  
  7         70  
12 7     7   19 use warnings ();
  7         8  
  7         87  
13 7     7   23 use feature ();
  7         10  
  7         77  
14 7     7   897 use utf8 ();
  7         475  
  7         132  
15 7     7   2579 use true ();
  7         46951  
  7         191  
16              
17 7     7   2871 use Feature::Compat::Try ();
  7         2095  
  7         147  
18 7     7   2536 use builtin::compat ();
  7         97771  
  7         5156  
19              
20             my @FEATURES = qw(
21             say
22             state
23             fc
24             );
25              
26             my @BUILTINS = qw(
27             blessed
28             refaddr
29             reftype
30             trim
31             ceil
32             floor
33             true
34             false
35             weaken
36             unweaken
37             is_weak
38             );
39              
40             my %KNOWN_FLAG = map { $_ => 1 } qw(
41             -utf8
42             -class
43             -defer
44             -corinna
45             -always_true
46             );
47              
48             my %KNOWN_HASH_KEY = map { $_ => 1 } qw(
49             utf8
50             class
51             defer
52             corinna
53             always_true
54             );
55              
56             sub import {
57 24     24   24129 my ($class, @args) = @_;
58 24         83 my $target = caller;
59 24         104 my $config = _parse_args(@args);
60              
61 21         46 _validate_config($config);
62              
63 19         90 strict->import::into($target);
64 19         3421 warnings->import::into($target);
65              
66 19         4671 feature->import::into($target, @FEATURES);
67              
68 19         4096 Feature::Compat::Try->import::into($target);
69              
70 19         3535 builtin::compat->import::into($target, @BUILTINS);
71              
72 19 100       5504 utf8->import::into($target) if $config->{utf8};
73              
74 19 100       1148 _set_always_true(1) if $config->{always_true};
75              
76             _import_optional_module($target, 'Feature::Compat::Class', $config->{class})
77 19 100       51 if $config->{class};
78              
79             _import_optional_module($target, 'Feature::Compat::Defer', $config->{defer})
80 19 100       39 if $config->{defer};
81              
82             _import_optional_module($target, 'Object::Pad', $config->{corinna})
83 19 100       43 if $config->{corinna};
84              
85 19         1420 return;
86             }
87              
88             sub unimport {
89 9     9   49 my ($class, @args) = @_;
90 9         18 my $target = caller;
91 9         23 my $config = _parse_args(@args);
92              
93 8         17 _validate_config($config);
94              
95 8         30 strict->unimport::out_of($target);
96 8         1341 warnings->unimport::out_of($target);
97              
98 8         1176 feature->unimport::out_of($target, @FEATURES);
99 8         1209 utf8->unimport::out_of($target);
100              
101 8 100       1042 _set_always_true(0) if $config->{always_true};
102              
103 8         220 return;
104             }
105              
106             sub _parse_args {
107 33     33   54 my (@args) = @_;
108              
109 33 100       106 return {} unless @args;
110              
111 24 100 100     97 if (@args == 1 && ref($args[0]) eq 'HASH') {
112 12         27 return _parse_hash_args($args[0]);
113             }
114              
115 12         31 return _parse_flag_args(@args);
116             }
117              
118             sub _parse_flag_args {
119 12     12   18 my (@args) = @_;
120 12         30 my %config;
121              
122 12         22 for my $arg (@args) {
123 19 100       40 die __PACKAGE__ . qq{: hash-style arguments must be passed as a single hash reference\n}
124             if ref $arg;
125              
126             die __PACKAGE__ . qq{: unknown import option "$arg"\n}
127 18 100       60 unless $KNOWN_FLAG{$arg};
128              
129 16         41 (my $key = $arg) =~ s/^-//;
130 16         33 $config{$key} = 1;
131             }
132              
133 9         20 return \%config;
134             }
135              
136             sub _parse_hash_args {
137 12     12   16 my ($raw) = @_;
138 12         14 my %config = %{$raw};
  12         34  
139              
140 12         29 for my $key (keys %config) {
141             die __PACKAGE__ . qq{: unknown import key "$key"\n}
142 17 100       71 unless $KNOWN_HASH_KEY{$key};
143             }
144              
145 11         31 return \%config;
146             }
147              
148             sub _validate_config {
149 29     29   46 my ($config) = @_;
150              
151             die __PACKAGE__ . qq{: options "-class" and "-corinna" are mutually exclusive\n}
152 29 100 100     104 if $config->{class} && $config->{corinna};
153              
154 27         58 return;
155             }
156              
157             sub _set_always_true {
158 6     6   11 my ($enabled) = @_;
159              
160 6 100       12 if ($enabled) {
161 5         21 true->import();
162             }
163             else {
164 1         4 true->unimport();
165             }
166              
167 6         4168 return;
168             }
169              
170             sub _import_optional_module {
171 9     9   17 my ($target, $module, $opts) = @_;
172              
173 9         34 (my $file = "$module.pm") =~ s{::}{/}g;
174 9         2025 require $file;
175              
176 9 100       8629 if (ref($opts) eq 'HASH') {
177 2         4 $module->import::into($target, %{$opts});
  2         15  
178             }
179             else {
180 7         30 $module->import::into($target);
181             }
182              
183 9         1818 return;
184             }
185              
186             1;
187              
188             __END__