File Coverage

blib/lib/Modern/Perl/Prelude.pm
Criterion Covered Total %
statement 82 82 100.0
branch 22 22 100.0
condition 6 6 100.0
subroutine 17 17 100.0
pod n/a
total 127 127 100.0


line stmt bran cond sub pod time code
1             package Modern::Perl::Prelude;
2              
3 6     6   1553601 use v5.30;
  6         47  
4 6     6   26 use strict;
  6         10  
  6         151  
5 6     6   21 use warnings;
  6         27  
  6         439  
6              
7             # ABSTRACT: Project prelude for modern Perl style on Perl 5.30+
8             our $VERSION = '0.007';
9              
10 6     6   2422 use Import::Into ();
  6         14646  
  6         123  
11 6     6   35 use strict ();
  6         11  
  6         63  
12 6     6   17 use warnings ();
  6         9  
  6         58  
13 6     6   37 use feature ();
  6         8  
  6         97  
14 6     6   1092 use utf8 ();
  6         545  
  6         112  
15              
16 6     6   2536 use Feature::Compat::Try ();
  6         1885  
  6         110  
17 6     6   2116 use builtin::compat ();
  6         83961  
  6         4031  
18              
19             my @FEATURES = qw(
20             say
21             state
22             fc
23             );
24              
25             my @BUILTINS = qw(
26             blessed
27             refaddr
28             reftype
29             trim
30             ceil
31             floor
32             true
33             false
34             weaken
35             unweaken
36             is_weak
37             );
38              
39             my %KNOWN_FLAG = map { $_ => 1 } qw(
40             -utf8
41             -class
42             -defer
43             -corinna
44             );
45              
46             my %KNOWN_HASH_KEY = map { $_ => 1 } qw(
47             utf8
48             class
49             defer
50             corinna
51             );
52              
53             sub import {
54 19     19   22324 my ($class, @args) = @_;
55 19         40 my $target = caller;
56 19         42 my $config = _parse_args(@args);
57              
58 16         37 _validate_config($config);
59              
60 14         75 strict->import::into($target);
61 14         2396 warnings->import::into($target);
62              
63 14         2052 feature->import::into($target, @FEATURES);
64              
65 14         3003 Feature::Compat::Try->import::into($target);
66              
67 14         2576 builtin::compat->import::into($target, @BUILTINS);
68              
69 14 100       3989 utf8->import::into($target) if $config->{utf8};
70              
71             _import_optional_module($target, 'Feature::Compat::Class', $config->{class})
72 14 100       757 if $config->{class};
73              
74             _import_optional_module($target, 'Feature::Compat::Defer', $config->{defer})
75 14 100       33 if $config->{defer};
76              
77             _import_optional_module($target, 'Object::Pad', $config->{corinna})
78 14 100       33 if $config->{corinna};
79              
80 14         1057 return;
81             }
82              
83             sub unimport {
84 8     8   49 my ($class, @args) = @_;
85 8         14 my $target = caller;
86 8         18 my $config = _parse_args(@args);
87              
88 7         17 _validate_config($config);
89              
90 7         39 strict->unimport::out_of($target);
91 7         1218 warnings->unimport::out_of($target);
92              
93 7         984 feature->unimport::out_of($target, @FEATURES);
94 7         1048 utf8->unimport::out_of($target);
95              
96 7         961 return;
97             }
98              
99             sub _parse_args {
100 27     27   45 my (@args) = @_;
101              
102 27 100       98 return {} unless @args;
103              
104 18 100 100     72 if (@args == 1 && ref($args[0]) eq 'HASH') {
105 8         18 return _parse_hash_args($args[0]);
106             }
107              
108 10         23 return _parse_flag_args(@args);
109             }
110              
111             sub _parse_flag_args {
112 10     10   20 my (@args) = @_;
113 10         12 my %config;
114              
115 10         22 for my $arg (@args) {
116 15 100       33 die __PACKAGE__ . qq{: hash-style arguments must be passed as a single hash reference\n}
117             if ref $arg;
118              
119             die __PACKAGE__ . qq{: unknown import option "$arg"\n}
120 14 100       54 unless $KNOWN_FLAG{$arg};
121              
122 12         29 (my $key = $arg) =~ s/^-//;
123 12         26 $config{$key} = 1;
124             }
125              
126 7         15 return \%config;
127             }
128              
129             sub _parse_hash_args {
130 8     8   14 my ($raw) = @_;
131 8         8 my %config = %{$raw};
  8         28  
132              
133 8         18 for my $key (keys %config) {
134             die __PACKAGE__ . qq{: unknown import key "$key"\n}
135 10 100       33 unless $KNOWN_HASH_KEY{$key};
136             }
137              
138 7         19 return \%config;
139             }
140              
141             sub _validate_config {
142 23     23   32 my ($config) = @_;
143              
144             die __PACKAGE__ . qq{: options "-class" and "-corinna" are mutually exclusive\n}
145 23 100 100     79 if $config->{class} && $config->{corinna};
146              
147 21         28 return;
148             }
149              
150             sub _import_optional_module {
151 6     6   12 my ($target, $module, $opts) = @_;
152              
153 6         20 (my $file = "$module.pm") =~ s{::}{/}g;
154 6         1537 require $file;
155              
156 6 100       7993 if (ref($opts) eq 'HASH') {
157 2         2 $module->import::into($target, %{$opts});
  2         15  
158             }
159             else {
160 4         17 $module->import::into($target);
161             }
162              
163 6         1295 return;
164             }
165              
166             1;
167              
168             __END__