Full/Pragmata.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.4% 34 / 36 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Full/Script.pm ↑ Summary Full.pm »
1 package Full::Pragmata;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '1.004'; # VERSION
7 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Full::Pragmata - common pragmata for Perl scripts and modules
16  
17 =head1 SYNOPSIS
18  
19 # in your script or module:
20 use Full::Pragmata;
21 # use strict, warnings, utf8 etc. are all now applied and in scope
22  
23 =head1 DESCRIPTION
24  
25 Perl has many modules and features, including some features which are somewhat discouraged
26 in recent code.
27  
28 This module attempts to provide a good set of functionality for writing code without too
29 many lines of boilerplate. It has been extracted from L<Myriad::Class> so that it can be
30 used in other code without pulling in too many irrelevant dependencies.
31  
32 =head2 Language features
33  
34 The following Perl language features and modules are applied to the caller:
35  
36 =over 4
37  
38 =item * L<strict>
39  
40 =item * L<warnings>
41  
42 =item * L<utf8>
43  
44 =item * L<perlsub/signatures>
45  
46 =item * no L<indirect>
47  
48 =item * no L<multidimensional>
49  
50 =item * no L<bareword::filehandles>
51  
52 =item * L<Syntax::Keyword::Try>
53  
54 =item * L<Syntax::Keyword::Dynamically>
55  
56 =item * L<Syntax::Keyword::Defer> - or the standard Perl built-in defer since C< :v2 >
57  
58 =item * L<Syntax::Operator::Equ> - added in C< :v2 >
59  
60 =item * L<Future::AsyncAwait>
61  
62 =item * L<Future::AsyncAwait::Hooks> - added in C< :v2 >
63  
64 =item * provides L<Scalar::Util/blessed>, L<Scalar::Util/weaken>, L<Scalar::Util/refaddr>
65  
66 =item * provides L<List::Util/min>, L<List::Util/max>, L<List::Util/sum0>
67  
68 =item * provides L<List::Util/uniqstr> - added in C< :v2 >
69  
70 =item * provides L<List::Keywords/any>, L<List::Keywords/all>
71  
72 =item * provides L<JSON::MaybeUTF8/encode_json_text>, L<JSON::MaybeUTF8/encode_json_utf8>,
73 L<JSON::MaybeUTF8/decode_json_text>, L<JSON::MaybeUTF8/decode_json_utf8>, L<JSON::MaybeUTF8/format_json_text>
74  
75 =item * provides L<Unicode::UTF8/encode_utf8>, L<Unicode::UTF8/decode_utf8>
76  
77 =back
78  
79 In addition, the following core L<feature>s are enabled:
80  
81 =over 4
82  
83 =item * L<bitwise|feature>
84  
85 =item * L<current_sub|feature>
86  
87 =item * L<evalbytes|feature>
88  
89 =item * L<fc|feature>
90  
91 =item * L<postderef_qq|feature>
92  
93 =item * L<state|feature>
94  
95 =item * L<unicode_eval|feature>
96  
97 =item * L<unicode_strings|feature>
98  
99 =back
100  
101 =head2 Constraints and checks
102  
103 L<Data::Checks> is imported with the following constraints available:
104  
105 =over 4
106  
107 =item * Defined
108  
109 =item * Object
110  
111 =item * Str
112  
113 =item * Num
114  
115 =item * StrEq
116  
117 =item * NumGT
118  
119 =item * NumGE
120  
121 =item * NumLE
122  
123 =item * NumLT
124  
125 =item * NumRange
126  
127 =item * NumEq
128  
129 =item * Isa
130  
131 =item * ArrayRef
132  
133 =item * HashRef
134  
135 =item * Callable
136  
137 =item * Maybe
138  
139 =item * Any
140  
141 =item * All
142  
143 =back
144  
145 Note that L<Data::Checks> provides the underlying support for constraints, but
146 actual usage involves a combination of other modules:
147  
148 =head3 Field constraints
149  
150 These are supported through L<Object::Pad::FieldAttr::Checked>:
151  
152 package Example;
153 use Full::Class qw(:v2);
154 field $checked :Checked(Str);
155  
156 =head3 Other features
157  
158 This also makes available a L<Log::Any> instance in the C<$log> package variable,
159 and for L<OpenTelemetry> support you get C<$tracer> as an L<OpenTelemtry::Tracer>
160 instance.
161  
162 =head2 VERSIONING
163  
164 A version tag is required:
165  
166 use Full::Pragmata qw(:v1);
167  
168 Currently C<:v1> is the only version available. It's very likely that future versions
169 will bring in new functionality or enable/disable a different featureset, or may
170 remove functionality or behaviour that's no longer appropriate.
171  
172 =cut
173  
174 8 ▶no indirect qw(fatal);
Subroutine BEGIN: called CC=1 SCAR=0.0
175 8 ▶no multidimensional;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 8 ▶no bareword::filehandles;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 8 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 8 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
179 0 ▶use meta;
Subroutine BEGIN: called CC=1 SCAR=2.6
180 no warnings qw(meta::experimental);
181 use curry;
182 use Data::Checks;
183 use Object::Pad::FieldAttr::Checked;
184 use Sublike::Extended;
185 use Signature::Attribute::Checked;
186 use Future::AsyncAwait;
187 use Future::AsyncAwait::Hooks;
188 use Syntax::Keyword::Try;
189 use Syntax::Keyword::Dynamically;
190 use Syntax::Keyword::Defer;
191 use Syntax::Keyword::Match;
192 use Syntax::Operator::Equ;
193 use Scalar::Util;
194 use List::Util;
195 use List::Keywords;
196 use Future::Utils;
197 use Module::Load ();
198  
199 use JSON::MaybeUTF8;
200 use Unicode::UTF8;
201  
202 use Log::Any qw($log);
203 use Metrics::Any;
204  
205 use constant USE_OPENTELEMETRY => $ENV{USE_OPENTELEMETRY};
206  
207 BEGIN {
208 if(USE_OPENTELEMETRY) {
209 require OpenTelemetry;
210 require OpenTelemetry::Context;
211 require OpenTelemetry::Trace;
212 require OpenTelemetry::Constants;
213 }
214 }
215  
216 sub import ($called_on, $version_tag, %args) {
217 die "invalid version, expecting something like use @{[caller]} qw(:v1);"
218 unless my $version = $version_tag =~ /^:v([0-9]+)/;
219  
220 my $pkg = $args{target} // caller(0);
221  
222 my $class = __PACKAGE__;
223  
224 # Apply core syntax and rules
225 strict->import;
226 warnings->import;
227 utf8->import;
228  
229 # We want mostly the 5.36 featureset, but since that includes `say` and `switch`
230 # we need to customise the list somewhat
231 feature->import(qw(
232 bitwise
233 current_sub
234 evalbytes
235 fc
236 postderef_qq
237 state
238 unicode_eval
239 unicode_strings
240 ));
241  
242 # Indirect syntax is problematic due to `unknown_sub { ... }` compiling and running
243 # the block without complaint, and only failing at runtime *after* the code has
244 # executed once - particularly unfortunate with try/catch
245 indirect->unimport(qw(fatal));
246 # Multidimensional array access - $x{3,4} - is usually a sign that someone wanted
247 # `@x{3,4}` or similar instead, so we disable this entirely
248 multidimensional->unimport;
249 # Plain STDIN/STDOUT/STDERR are still allowed, although hopefully never used by
250 # service code - new filehandles need to be lexical.
251 bareword::filehandles->unimport;
252  
253 # This one's needed for nested scope, e.g. { package XX; use Full::Service; method xxx (%args) ... }
254 experimental->import('signatures');
255  
256 # We don't really care about diamond inheritance, since microservices are expected
257 # to have minimal inheritance in the first place, but might as well have a standard
258 # decision to avoid surprises in future
259 mro::set_mro($pkg => 'c3');
260  
261 # Helper functions which are used often enough to be valuable as a default
262 Scalar::Util->export($pkg => qw(refaddr blessed weaken));
263 List::Util->export($pkg => qw(min max sum0));
264  
265 # Additional features in :v2 onwards
266 List::Util->export($pkg => qw(uniqstr));
267 # eval "package $pkg; use Object::Pad::FieldAttr::Checked; use Data::Checks qw(NumGE); 1" or die $@;
268 Object::Pad::FieldAttr::Checked->import($pkg);
269 Sublike::Extended->import;
270 Signature::Attribute::Checked->import($pkg);
271 Data::Checks->import(qw(
272 Defined
273 Object
274 Str
275 Num
276 StrEq
277 NumGT
278 NumGE
279 NumLE
280 NumLT
281 NumRange
282 NumEq
283 Isa
284 ArrayRef
285 HashRef
286 Callable
287 Maybe
288 Any
289 All
290 ));
291  
292 {
293 no strict 'refs';
294 *{$pkg . '::' . $_} = JSON::MaybeUTF8->can($_) for qw(
295 encode_json_text
296 encode_json_utf8
297 decode_json_text
298 decode_json_utf8
299 format_json_text
300 );
301 *{$pkg . '::' . $_} = Unicode::UTF8->can($_) for qw(
302 encode_utf8
303 decode_utf8
304 );
305 }
306 {
307 no strict 'refs';
308 *{$pkg . '::' . $_} = Future::Utils->can($_) for qw(
309 fmap_void
310 fmap_concat
311 fmap_scalar
312 fmap0
313 fmap1
314 );
315 }
316  
317 {
318 no strict 'refs';
319 # trim() might appear in core perl at some point, so let's reserve the
320 # word and include a basic implementation first. Avoiding Text::Trim
321 # here because it sometimes returns an empty list, which would be
322 # dangerous - my %hash = (key => trim($value)) for example.
323 *{$pkg . '::trim'} = sub ($txt) {
324 return undef unless defined $txt;
325 $txt =~ s{^\s+}{};
326 $txt =~ s{\s+$}{};
327 return $txt;
328 };
329 }
330  
331 # Some well-designed modules provide direct support for import target
332 Syntax::Keyword::Try->import_into($pkg, try => ':experimental(typed)');
333 Syntax::Keyword::Dynamically->import_into($pkg);
334 Syntax::Keyword::Defer->import_into($pkg);
335 Syntax::Operator::Equ->import_into($pkg);
336 Future::AsyncAwait->import_into($pkg, ':experimental(cancel)');
337 Metrics::Any->import_into($pkg, '$metrics');
338  
339 Future::AsyncAwait::Hooks->import_into($pkg);
340  
341 # Others use lexical hints
342 List::Keywords->import(qw(any all));
343 Syntax::Keyword::Match->import(qw(match));
344  
345 {
346 no strict 'refs';
347 if(USE_OPENTELEMETRY) {
348 my $provider = OpenTelemetry->tracer_provider;
349 *{$pkg . '::tracer'} = \($provider->tracer(
350 name => $args{app} // 'perl',
351 version => $args{version} // $version,
352 ));
353 }
354 *{$pkg . '::log'} = \Log::Any->get_logger(
355 category => $pkg
356 );
357 }
358  
359 return $pkg;
360 }
361  
362 1;
363  
364 __END__
« Full/Script.pm ↑ Summary Full.pm »