Sympatic.pm

filter: Statementstmt 88.8% 32 / 36 Branchbran 72.2% 13 / 18 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 85.0% 51 / 60 CC 12 SCAR 24.2
CC
12
Cov
84%
import
13.0
BEGIN
1.0
BEGIN
1.0
CRAP 11.3
↑ Summary Sympatic/Role.pm »
1 package Sympatic;
2 7 our $VERSION = '0.2';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 require Import::Into;
6  
7 8 ▶sub import {
Subroutine import: called CC=12 SCAR=25.7
8  
9 8 my $to; # the NS to infect
10 # Sympatic->import(@options)
11 # Sympatic->import(to => $NS, @options)
12 8 ▶ ('to' eq ( $_[1] // '' ))
Branch: 1 branch
BranchTrueFalseTotal
'to' eq ($_[1] // '') ? : 1 7 8
Condition100%2/2
$_[1] // ""
l!l
35
MC/DC100%1/1
$_[1] // ""
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
13 ? ( $to = $_[2], splice @_,0,3 )
14 : ( $to = caller, shift );
15  
16 8 my %feature = qw<
17 utf8all .
18 utf8 .
19 utf8io .
20 oo .
21 class .
22 path .
23 >;
24  
25 8 English->import::into( $to, qw< -no_match_vars > );
26 8 feature->import::into( $to, qw< say state > );
27 8 strict->import::into($to);
28 8 warnings->import::into($to);
29 8 Function::Parameters->import::into($to);
30  
31 8 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 8 11
32  
33 # disable default features
34 3 ▶ if ( $_[0] =~ /-(?<feature>
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /-(?<feature> utf8all | utf8 | utf8io | oo | class | path )/x) 3 0 3
35 utf8all |
36 utf8 |
37 utf8io |
38 oo |
39 class |
40 path
41 )/x) {
42 3 delete $feature{ $+{feature} };
43 3 shift;
44 3 next;
45 }
46  
47 ...
48  
49 0 }
50  
51 8 ▶ $feature{path} and do { Path::Tiny->import::into($to) };
Branch: 1 branch
BranchTrueFalseTotal
$feature{"path"} and do { "Path::Tiny"->import::into($to) } 8 0 8
52  
53 8 ▶ $feature{oo} and do {
Branch: 1 branch
BranchTrueFalseTotal
$feature{"oo"} and do { ($feature{"class"} ? "Moo" : "Moo::Role")->import::into($to); "MooX::LvalueAttribute"->import::into($to) } 6 2 8
54 ( $feature{class}
55 6 ▶ ? 'Moo'
Branch: 1 branch
BranchTrueFalseTotal
$feature{'class'} ? : 5 1 6
56 : 'Moo::Role' )->import::into($to);
57 6 MooX::LvalueAttribute->import::into($to);
58 };
59  
60 8 ▶ $feature{utf8all} and do {
Branch: 1 branch
BranchTrueFalseTotal
$feature{"utf8all"} and do { "utf8::all"->import::into($to); delete $feature{$_} foreach ("utf8", "utf8io") } 8 0 8
61 8 utf8::all->import::into($to);
62 8 delete $feature{$_} for qw< utf8 utf8io >;
63 };
64  
65 8 ▶ $feature{utf8} and do {
Branch: 1 branch
BranchTrueFalseTotal
$feature{"utf8"} and do { "utf8"->import::into($to); "feature"->import::into($to, "unicode_strings") } 0 8 8
66 0 utf8->import::into($to);
67 0 feature->import::into( $to, qw< unicode_strings > );
68 };
69  
70 0 ▶ $feature{utf8io} and do { 'open'->import::into( $to, qw< :UTF-8 :std > ) };
Branch: 1 branch
BranchTrueFalseTotal
$feature{"utf8io"} and do { "open"->import::into($to, (":UTF-8", ":std")) } 0 8 8
71  
72 # see https://github.com/pjf/autodie/commit/6ff9ff2b463af3083a02a7b5a2d727b8a224b970
73 # TODO: is there a case when caller > 1 ?
74  
75 # $feature{autodie} and do {
76 # autodie->import::into(1);
77 # }
78  
79 }
80  
81 7 1;
82  
83 =encoding utf8
84  
85 =head1 NAME
86  
87 Sympatic - A more producive perl thanks to CPAN
88  
89 =head1 STATUS
90  
91 =for HTML
92 <a href="http://travis-ci.org/sympa-community/p5-sympatic/">
93 <img src="https://travis-ci.org/sympa-community/p5-sympatic.svg?branch=master">
94 </a>
95  
96 Any bug report or feedback that can help to improve C<Sympatic> are very welcome.
97 The quickest way to report a bug in Sympatic is by sending email to
98 bug-Sympatic [at] rt.cpan.org. You can also report from the web using
99 L<CPAN RT|https://rt.cpan.org/Public/Bug/Report.html?Queue=Sympatic> or even
100 L<Github|https://github.com/sympa-community/p5-sympatic/issues>.
101  
102 =head1 SYNOPSIS
103  
104 package Counter;
105 use Sympatic;
106  
107 use Types::Standard qw< Int >;
108  
109 has qw( value is rw )
110 , default => 0
111 , lvalue => 1
112 , isa => Int;
113  
114 method next { ++$self->value }
115 method ( Int $add ) { $self->value += $add }
116  
117 see L</USAGE> section for more details.
118  
119 =head1 DESCRIPTION
120  
121 The default behavior of L<Perl|http://www.perl.org> could be significantly
122 improved by the pragmas and CPAN modules so it can fit the expectations
123 of a community of developers and help them to enforce what they consider
124 as the best practices. For decades, the minimal boilerplate seems to be
125  
126 use strict;
127 use warnings;
128  
129 This boilerplate can evolve over time to be much larger. Fortunately, it
130 can be embedded into a module. Sympatic is the boilerplate module for
131 the L<Sympa project|http://www.sympa.org> project.
132  
133 Some of the recommendations are inspired by the
134 L<Perl Best Practices|http://shop.oreilly.com/product/9780596001735.do>
135 book from L<Damian Conway|http://damian.conway.org/> (known as PBP in this document).
136  
137 =head2 The goals behind Sympatic
138  
139 This section describes the goals that leads to the choices made for Sympatic and
140 the coding style recommendations.
141  
142 =head3 No one left behind
143  
144 As we try to avoid leaving anyone behind, we also need to think about the future.
145  
146 As some sympa servers run on quite old unix systems, we try to make our code
147 run on old versions of the perl interpreters. However, this should not
148 take us away from features of recent versions of perl that really help
149 performances, stability or good coding practices.
150  
151 We are currently supporting all the versions of perl since perl 5.16
152 (released the 2012-May-2). That's the best we can afford. Please contact us
153 if you need support for older Perl.
154  
155 =head3 Reduce infrastructure code
156  
157 As perl emphasizes freedom, it leaves you on your own with minimal tooling
158 to write such simple and standard things most of us don't want to write by
159 hand anymore (like object properties getters and setters, function parameter
160 checkings, ...). This code is described by Damian Conway as "the infrastructure
161 code".
162  
163 CPAN provide very good modules to make those disappear and we picked the ones
164 we think to be the most relevant. Putting them all together provides the ability
165 to write very expressive code without sacrifying the power of Perl.
166  
167 =head3 Make perl more familiar for newcommers
168  
169 Choosing the CPAN modules to reduce infrastructure codes and writing the coding
170 style recommendation below was made with our friends from the other dynamic langages
171 in mind. We really expect developers from the ruby, javascript and python to have
172 a much better experience using Sympatic as it provides some idioms close to the ones
173 they know in addition of the unique perl features.
174  
175 =head3 Less typing and opt out policy
176  
177 Sympatic has the ability to provide different sets of features
178 (see C<features> section) and the ones that are imported by default
179 are the one that are used in the most common cases. For exemple: as
180 most of the sympa packages actually are objects, the L<Moo|Moo> keywords
181 are exported by default.
182  
183 See the L<features|features> section to learn how to avoid some of them.
184  
185 =head2 What using Sympatic means?
186  
187 If you are an experimented Perl developer, the simplest way to
188 introduce Sympatic is to say that
189  
190 use Sympatic;
191  
192 is equivalent to
193  
194 use strict;
195 use warnings;
196 use feature qw< unicode_strings say state >;
197 use English qw< -no_match_vars >;
198 use utf8;
199 use Function::Parameters;
200 use Moo;
201  
202 If you're not, we highly recommend the well written L<Perl
203 Documentation|http://perldoc.perl.org> (the `*tut` sections).
204 Here we provide a very short description
205  
206 The L<utf8|https://perldoc.perl.org/utf8.html> pragma makes perl aware
207 that the code of your project is encoded in utf8.
208  
209 The L<strict|https://perldoc.perl.org/strict.html> pragma avoid the
210 the perl features requiring too much caution. Also the
211 L<warnings|https://perldoc.perl.org/warnings.html> one provides very
212 informational messages when perl detects a potential mistake. You can
213 use L<diagnostics|https://perldoc.perl.org/diagnostics.html> to get a
214 direct reference to the perl manual when a warning or an error message is
215 raised.
216  
217 L<feature|https://metacpan.org/pod/feature> is the Perl pragma to enable new
218 features from new versions of the perl interpreter. If the perl interpreter
219 you are using is too old, you will get an explicit message about the missing
220 feature. Note that we use
221  
222 use feature qw< unicode_strings say state >;
223 use strict;
224 use warnings;
225  
226 instead of
227  
228 use v5.14;
229  
230 to avoid the use of features related to
231 L<smart match|https://perldoc.perl.org/perlop.html#Smartmatch-Operator>
232 like the L<given/when flow control|https://perldoc.perl.org/functions/given.html>
233 as they were abundantly criticized and will be removed in perl 5.28.
234  
235 L<English|https://metacpan.org/pod/English> - enable the english (named against
236 awk variables) names for the variables documented in
237 L<the perlvar manual|https://metacpan.org/pod/perlvar>.
238  
239 So basically, using C<Sympatic>, the two following instructions are the same.
240  
241 print $(;
242 print $GID;
243  
244 L<Function::Parameters|https://metacpan.org/pod/Function::Parameters> introduces
245 the keywords C<fun> and C<method> to allow function signatures with gradual typin,
246 named parameters and other features probably inspired by perl6, python and javascript.
247 See L<examples|examples> section.
248  
249 L<Types::Standard|https://metacpan.org/pod/Types::Standard> provides nice generic
250 way to define types that can be used from the C<fun> and C<method>
251 signatures or the C<isa> constraint of a Moo property declaration.
252  
253 =head1 USAGE
254  
255 =head2 Declaring functions
256  
257 In addition to the C<sub> keyword provided by perl (documented in the
258 L<perlsub|perlsub> manual), Sympatic comes with C<fun> and C<method>
259 (provided and documented in L<Function::Parameters|Function::Parameters>).
260  
261 As those two documents are very well written, the current documentation
262 only discuss about picking one of them and providing some examples.
263  
264 =for comment repetition of the last section ?
265  
266 Use C<fun> when you can provide a signature for a function. C<fun> provide
267 a signature syntax inspired by L<perl6|http://perl6.org/> so you can use positional and
268 named parameters, default values, parameter destructuring and gradual typing.
269 You should use it in most cases.
270  
271 Here are some examples:
272  
273 # positional parameter $x
274 fun absolute ( $x ) { $x < 0 ? -$x : $x }
275  
276 # typing
277 use Types::Standard qw< Int >;
278 fun absolute ( Int $x ) { $x < 0 ? -$x : $x }
279  
280 # default parameters
281 fun point ( $x = 0, $y = 0 ) { "( $x ; $y )" }
282 point 12; # ( 12 ; 0 )
283  
284 # named parameters
285 fun point3D ( :$x = 0, :$y = 0, :$z = 0 ) { "( $x ; $y ; $z )" }
286 say point3D x => 12; # ( 12 ; 0 ; 0 )
287  
288 Use the C<sub> keyword fully variadic functions (the parameters are stored in
289 the C<@_> array) or to use for example, let's assume you want to write a simple
290 CSV serializer usable like this
291  
292 print csv qw( header1 header2 header3 );
293 # outputs:
294 # header1;header2;header3
295  
296 This is a naive implementation demonstrating the use of C<@_>
297  
298 sub csv { ( join ';', @_ ) , "\n" }
299  
300  
301 Common cases are list reduction or partial application like
302  
303 sub price_with_taxes { price tax_rate => .2, @_ }
304  
305 =head3 Default perl signatures, prototypes and attributes
306  
307 Experienced perl programmers should note that we don't use the perl
308 signatures as documented in L<perlsub|perlsub> for two reasons:
309  
310 Those signatures appear as experimental in L<perl5.20|perlhist> and
311 are finally a feature in L<perl5.26|perlhist> with a changing behaviour
312 in L<perl5.26|perlhist> to make prototypes happy. Plus, we are bound
313 to L<perl5.16|perlhist>. Also, the signatures provided by
314 L<Function::Parameters|Function::Parameters>) are much more powerful than the
315 core ones (see description above).
316  
317 Attributes are still available. If you need to declare a prototype, they are available
318 using the C<:prototype()> attribute as described in the
319 L<OMGTODOFINDALINK|"signature" section of perlsub>. For exemple
320  
321 fun twice ( $block ) :prototype(&) { &$block; &$block }
322 twice {say "hello"}
323 # outputs:
324 # hello
325 # hello
326  
327 =head2 Object Oriented programming
328  
329 Sympatic imports L<Moo|Moo> and L<Function::Parameters> which means that
330 you can declare an object using
331  
332 =over
333  
334 =item
335  
336 C<has> to define a new property
337  
338 =item
339  
340 C<extends> to inherit from a super class
341  
342 =item
343  
344 C<with> to compose your class using roles
345  
346 =item
347  
348 C<method> to combine with roles
349  
350 =back
351  
352 TODO: that keywords like around, after ?
353  
354 use Sympatic;
355 use Types::Standard qw< Int >;
356  
357 has value
358 ( is => 'rw'
359 , isa => Int
360 , lvalue => 1
361 , default => 0 );
362  
363 method add ( Int $x ) { $self->value += $x }
364  
365 Note that the method C<add()> is almost useless when C<< $self->value >> is lvalue.
366  
367 package Human;
368 use Sympatic;
369 use Types::Standard qw< InstanceOf Str >;
370  
371 has qw( name is rw )
372 , isa => Str;
373  
374 method greetings ( (InstanceOf['Human']) $other ) {
375 sprintf "hello %s, i'm %s and i want to be a friend of you"
376 , $self->name
377 , $other->name
378 }
379  
380 =head2 Work with the filesystem
381  
382 The "all in one" C<path> helper from L<Path::Tiny|https://metacpan.org/pod/Path::Tiny>
383 is exported by Sympatic. Refer to the documentation for examples.
384  
385 =head2 set/unset features
386  
387 TODO: describe how to enable/disable features
388 TODO: describe the features themselves
389  
390 =head1 CONTRIBUTE
391  
392 Any kind of contribution that can help to improve C<Sympatic> and the
393 L<Sympa project|http://www.sympa.org> are very welcome. We meant *all* of them!
394 from donating to setting up an hackathon, make some goodies or visual
395 materials, webmastering, help promoting, translating, documenting, mentoring,
396 ... please contact L<us|http://www.sympa.org> on
397 L<the freenode #sympa channel|irc://moznet/mozillazine> or the
398 L<the sympa users mailing list|https://listes.renater.fr/sympa/info/sympa-users>.
399 French people can also join us in
400 L<the freenode #sympa channel|irc://moznet/mozillazine> or the
401 L<the sympa users mailing list|https://listes.renater.fr/sympa/info/sympa-users>.
402  
403 You are welcome to discuss about the C<Sympatic> style on the Sympa project
404 developers mailing list. If your proposal is accepted, edit the module the
405 way you describe, update the documentation and test the whole thing.
406  
407 cpanm --installdeps .
408 sh xt/bin/test_install_dist.sh
409  
410 =head1 Sympa and CPAN
411  
412 Every line of code that is used in the Sympa project should be carefully
413  
414 The CPAN community reduces the cost of maintaining infrastructure code. And
415 by maintaining it, we mean it the great way: improve, optimize, document,
416 debug, test in a large number of perl bases, ...
417  
418 We also want to benefit as much as possible from the experience, ideas and
419 knowledge of the CPAN members.
420  
421 So if you want to contribute to Sympa, please consider picking a module on CPAN
422 that does the job and contributing to it if needed. Push your own stuff if
423 needed.
424  
425 =head2 Other CPAN modules
426  
427 =head3 Those we also rely on
428  
429 L<Dancer2|https://metacpan.org/pod/Dancer2> for web development,
430 L<Template Toolkit|https://metacpan.org/pod/Template> for text templating,
431  
432 =head3 Those which can be useful too
433  
434  
435 L<Curry|https://metacpan.org/pod/Curry> eases the creation of
436 streams and callbacks.
437  
438 sub { $self->foo('bar') }
439  
440 can be written as
441  
442 $self->curry::foo('bar')
443  
444 =head2
445  
446 L<Perlude|https://metacpan.org/pod/Perlude> is the way to manipulate
447 and combine streams.
448  
449 =head1 AUTHORS
450  
451 Thanks to the people who contributed to the sympatic module (by date)
452  
453 =over
454  
455 =item Marc Chantreux
456  
457 =item David Verdin
458  
459 =item Mohammad S Anwar
460  
461 =item Stefan Hornburg (Racke)
462  
463 =back
464  
465 =head1 CONTACTS
466  
467 let's pick up the most confortable way for you
468  
469 =head1 IRC
470  
471 you can contact us via IRC on the main IRC channel (L<freenode #sympa
472 channel|irc://freenode/sympa>). The used langage is english but don't hésitate
473 to speak another one if you're not confortable enough. there is also a
474 (L<freenode #sympa-fr channel|irc://freenode/sympa>) for french people and you
475 are really welcome to create a new channel for your own langage (just let us
476 now).
477  
478 =head1 mailing lists
479  
480 pick the most relevant group there
481  
482 =over
483  
484 =item L<developers|https://listes.renater.fr/sympa/info/sympa-developpers>
485  
486 =item L<packagers|https://listes.renater.fr/sympa/info/sympa-packagers>
487  
488 =item L<security|https://listes.renater.fr/sympa/info/sympa-security>
489  
490 =back
491  
492 =head1 CONTRIBUTE
493  
494 =head2 join us
495  
496 Any kind of contribution that can help to improve C<Sympatic> and the
497 C<Sympa project|http://www.sympa.org> are very welcome. We meant *all* of them!
498 from donating to setting up an hackathon, make some goodies or visual
499 materials, webmastering, help promoting, translating, documenting, mentoring,
500 ... If you need help on helping us, don't hesitate to contact us. (see the contact section)
501  
502  
503 =head2 bug report and feedback
504  
505 any bugfixe, improvement, documentation, proposal to do? let's talk about it ...
506  
507 The quickest way to report a bug in Sympatic is by sending email to
508 bug-Sympatic [at] rt.cpan.org. You can also report from the web using
509 [CPAN RT|https://rt.cpan.org/Public/Bug/Report.html?Queue=Sympatic> or even
510 [Github|https://github.com/sympa-community/p5-sympatic/issues>.
511  
512 =head1 LICENSE AND COPYRIGHT
513  
514 Copyright 2018 Sympa community <F<sympa-developpers@listes.renater.fr>>
515  
516 This package is free software and is provided "as is" without express
517 or implied warranty. you can redistribute it and/or modify it under the
518 same terms as the Perl 5 programming language system itself.
519  
520 =head1 LICENCE
521  
522 Copyright (C) 2017,2018 Sympa Community
523  
524 Sympatic is free software; you can redistribute it and/or
525 modify it under the terms of the GNU General Public License as
526 published by the Free Software Foundation; either version 2 of the
527 License, or (at your option) any later version.
528  
529 Sympatic is distributed in the hope that it will be useful,
530 but WITHOUT ANY WARRANTY; without even the implied warranty of
531 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
532 General Public License for more details.
533  
534 You should have received a copy of the GNU General Public License
535 along with this program; if not, see <http://www.gnu.org/licenses/>.
536  
537 =cut
↑ Summary Sympatic/Role.pm »