File Coverage

blib/lib/Sympatic.pm
Criterion Covered Total %
statement 32 36 88.8
branch 7 12 58.3
condition n/a
subroutine 4 4 100.0
pod n/a
total 43 52 82.6


line stmt bran cond sub pod time code
1             package Sympatic;
2             our $VERSION = '0.1_002';
3 6     6   269686 use strict;
  6         35  
  6         178  
4 6     6   32 use warnings;
  6         11  
  6         1109  
5             require Import::Into;
6              
7             sub import {
8              
9 6     6   55 my $to = caller;
10 6         30 my %feature = qw<
11             utf8all .
12             utf8 .
13             utf8io .
14             oo .
15             path .
16             >;
17              
18 6         35 English->import::into($to, qw< -no_match_vars >);
19 6         21996 feature->import::into($to, qw< say state >);
20 6         1735 strict->import::into($to);
21 6         1166 warnings->import::into($to);
22 6         1066 Function::Parameters->import::into($to);
23              
24 6         28965 shift; # 'Sympatic', the package name
25              
26 6         30 while (@_) {
27              
28             # disable default features
29 2 50       15 if ( $_[0] =~
30             /- (?
31             utf8all |
32             utf8 |
33             utf8io |
34             oo |
35             path )/x
36             ) {
37 6     6   2786 delete $feature{ $+{feature} };
  6         2306  
  6         1389  
  2         19  
38 2         5 shift;
39 2         8 next;
40             }
41              
42             ...
43              
44 0         0 }
45              
46 6 50       25 $feature{path} and do { Path::Tiny->import::into($to) };
  6         38  
47 6 100       82172 $feature{oo} and do {
48 4         33 Moo->import::into($to);
49 4         47162 MooX::LvalueAttribute->import::into($to);
50             };
51              
52 6 50       143326 $feature{utf8all} and do {
53 6         55 utf8::all->import::into($to);
54 6         300499 delete $feature{$_} for qw< utf8 utf8io >;
55             };
56              
57 6 50       35 $feature{utf8} and do {
58 0         0 utf8->import::into($to);
59 0         0 feature->import::into($to, qw< unicode_strings >);
60             };
61              
62 6 50       106182 $feature{utf8io} and do { 'open'->import::into($to,qw< :UTF-8 :std >) };
  0            
63              
64             # see https://github.com/pjf/autodie/commit/6ff9ff2b463af3083a02a7b5a2d727b8a224b970
65             # TODO: is there a case when caller > 1 ?
66              
67             # $feature{autodie} and do {
68             # autodie->import::into(1);
69             # }
70              
71             }
72              
73             1;
74              
75             =encoding utf8
76              
77             =head1 NAME
78              
79             Sympatic - A more producive perl thanks to CPAN
80              
81             =head1 STATUS
82              
83             =for HTML
84            
85            
86            
87              
88             =head1 SYNOPSIS
89              
90             package Counter;
91             use Sympatic;
92              
93             use Types::Standard qw< Int >;
94              
95             has qw( value is rw )
96             , default => 0
97             , lvalue => 1
98             , isa => Int;
99              
100             method next { ++$self->value }
101             method ( Int $add ) { $self->value += $add }
102              
103             see L section for more details.
104              
105             =head1 DESCRIPTION
106              
107             This document is an introduction to Sympatic Perl,
108              
109             L developers community focus on
110              
111             The default behavior of L could be significantly
112             improved by the pragmas and CPAN modules so it can fit the expectations
113             of a community of developers and help them to enforce what they consider
114             as the best practices. For decades, the minimal boilerplate seems to be
115              
116             use strict;
117             use warnings;
118              
119             This boilerplate can evolve over the time be much more larger. Fortunately, it
120             can be embedded into a module.
121              
122             Sympatic is the boilerplate module for the L
123             project.
124              
125             Some of the recommendations are inspired by the L
126             Practices|http://shop.oreilly.com/product/9780596001735.do> book from L
127             Conway|http://damian.conway.org/>. We refer to this book as PBP in this document.
128              
129             =head2 the goals behind Sympatic
130              
131             This section describes the goals that leads to the choices made for Sympatic and
132             the coding style recommendations.
133              
134             =head3 balance between old servers and modern tools
135              
136             As we try to keep noone left behind, we also need to think about the future.
137              
138             As some sympa servers run on quiet old unix systems, we try to make our code
139             running on old versions of the perl interpreters. However, this should not
140             take us away from features of recent versions of perl that really helps
141             performances, stability or good coding practices.
142              
143             We are currently supporting all the versions of perl since perl 5.16
144             (released the 2012-May-2). That's the best we can afford. Please contact us
145             if you need support of older Perl.
146              
147             =head3 reduce infrastructure code
148              
149             As perl emphasize freedom, it leaves you on your own with minimal tooling
150             to write such simple and standard things most of us don't want to write by
151             hand anymore (like object properties getters and setters, function parameter
152             checkings, ...). This code is described by Damian Conway as "the infrastructure
153             code".
154              
155             CPAN provide very good modules to make those disapear and we picked the ones
156             we think to be the most relevant. Putting them all together provides the ability
157             to write very expressive code without sacrifying the power of Perl.
158              
159             =head3 make perl more familiar for newcommers
160              
161             Choosing the CPAN modules to reduce infrastructure codes and writing the coding
162             style recommendation below was made with our friends from the other dynamic langages
163             in mind. We really expect developpers from the ruby, javascript and python provide
164             a much better experience using Sympatic as it provides some idioms close to the ones
165             they know in adition of the unique perl features.
166              
167             =head3 less typing and opt out policy
168              
169             Sympatic has the ability to provide different sets of features
170             (see C section) and the ones that are imported by default
171             are the one that are used in the most common cases. For exemple: as
172             most of the sympa packages actually are objects, the L keywords
173             are exported by default.
174              
175             See the L section to learn how to avoid some of them.
176              
177             =head2 what use Sympatic means?
178              
179             If you are experimented Perl developpers, the simplest way to
180             introduce Sympatic is to say that
181              
182             use Sympatic;
183              
184             is equivalent to
185            
186             use strict;
187             use warnings;
188             use feature qw< unicode_strings say state >;
189             use English qw< -no_match_vars >;
190             use utf8;
191             use Function::Parameters;
192             use Moo;
193              
194             If you're not, we highly recommend the the well written L
195             Documentation|http://perldoc.perl.org> (the `*tut` sections).
196             Here we provide a very short description
197              
198             The L pragma makes perl aware
199             that the code of your project is encoded in utf8.
200              
201             The L pragma avoid the
202             the perl features requiring too much caution. Also the
203             L one provides very
204             informational messages when perl detects a potential mistake. You can
205             use L to get a
206             direct reference to the perl manual when a warning or an error message is
207             risen.
208              
209             L is the Perl pragma to enable new
210             features from new versions of the perl interpreter. If the perl interpreter
211             you are using is too old, you will get an explicit message about the missing
212             feature. Note that we use
213              
214             use feature qw< unicode_strings say state >;
215             use strict;
216             use warnings;
217              
218             instead of
219              
220             use v5.14;
221              
222             to avoid the use of features related to
223             L
224             like the L
225             as they were abundantly criticized and will be removed in perl 5.28.
226              
227             L - enable the english (named against
228             awk variables) names for the variables documented in
229             L.
230              
231             so basically, using C, the two following instructions are the same.
232              
233             print $(;
234             print $GID;
235              
236             L introduce
237             the keywords C and C to allow function signatures with gradual typin,
238             named parameters and other features probably inspired by perl6, python and javascript.
239             See L section.
240              
241             L provide nice generic
242             way to define types that can be installed used from the C and C
243             signatures or the C contraint of a Moo property declaration.
244              
245             =head1 USAGE
246              
247             =head2 declaring functions
248              
249             In addition to the C keyword provided by perl (documented in the
250             L manual), Sympatic comes with C and C
251             (provided and documented in L).
252              
253             As those two documentations are very well written, the current documentation
254             only discuss about picking one of them and providing some exemples.
255              
256             Use C when you can provide a signature for a function. C provide
257             a signature syntax inspired by L so you can use positional and
258             named parameters, default values, parameter destructuring and gradual typing.
259             You should use it in most cases.
260              
261             Here are some examples:
262              
263             # positional parameter $x
264             fun absolute ( $x ) { $x < 0 ? -$x : $x }
265              
266             # typing
267             use Types::Standard qw< Int >;
268             fun absolute ( Int $x ) { $x < 0 ? -$x : $x }
269              
270             # default parameters
271             fun point ( $x = 0, $y = 0 ) { "( $x ; $y )" }
272             point 12; # ( 12 ; 0 )
273              
274             # named parameters
275             fun point3D ( :$x = 0, :$y = 0, :$z = 0 ) { "( $x ; $y ; $z )" }
276             say point3D x => 12; # ( 12 ; 0 ; 0 )
277              
278             Use the C keyword fully variadic functions (the parameters are stored in
279             the C<@_> array) or to use for example, let's assume you want to write a simple
280             CSV seriralizer usable like this
281              
282             print csv qw( header1 header2 header3 );
283             # outputs:
284             # header1;header2;header3
285              
286             this is a naive implementation demonstrating the use of C<@_>
287              
288              
289             sub csv { ( join ';', @_ ) , "\n" }
290              
291              
292             common cases are list reduction or partial application
293              
294             =head3 default perl signatures, prototypes and attributes
295              
296             Experienced perl programmers should note that note that we don't use the perl
297             signatures as documented in L for two reasons:
298              
299             Those signatures are appears as experimental in L and
300             are finally L (as we are bound to L).
301             Also, the signatures provided by L) are
302             much more powerful than the core ones (see description above).
303              
304             Attributes are still available. If you need to declare a prototype, they are available
305             using the C<:prototype()> attribute as described in the
306             L. For exemple
307              
308             fun twice ( $block ) :prototype(&) { &$block; &$block }
309             twice {say "hello"}
310             # outputs:
311             # hello
312             # hello
313              
314             =head2 Object Oriented programming
315              
316             Sympatic imports L and L which means that
317             you can declare an object using
318              
319             =over
320              
321             =item
322              
323             C to define a new property
324              
325             =item
326              
327             C to inherit from a super class
328              
329             =item
330              
331             C to compose your class using roles
332              
333             =item
334              
335             C to combine with roles
336              
337             =back
338              
339             TODO: that keywords like around, after ?
340              
341             use Sympatic;
342             use Type::Standard qw< Int >;
343              
344             has value
345             ( is => 'rw'
346             , isa => Int
347             , lvalue => 1
348             , default => 0 );
349              
350             method add ( Int $x ) { $self->value += $x }
351              
352             Note that the method C is almost useless when C<$self->value> is lvalue.
353              
354             package Human;
355             use Sympatic;
356             use Type::Standard qw< InstanceOf Str >;
357              
358             has qw( name is rw )
359             , isa => Str;
360              
361             method greetings ( (InstanceOf['Human']) $other ) {
362             sprintf "hello %s, i'm %s and i want to be a friend of you"
363             , $self->name
364             , $other->name
365             }
366              
367             =head2 work with the filesystem
368              
369             the "all in one" C helper from L
370             is exported by Sympatic. refer to the documentation for examples.
371              
372             =head2 set/unset features
373              
374             TODO: describe how to enable/disable features
375             TODO: describe the features themselves
376              
377             =head1 CONTRIBUTE
378              
379             you are welcome to discuss about the C style on the Sympa project
380             developpers mailing list. If your proposal is accepted, edit the module the
381             way you describe, update the documentation and test the whole thing.
382              
383             cpanm --installdeps .
384             RELEASE_TESTING=1 prove -Ilib t
385              
386             =head1 Sympa and CPAN
387              
388             Every line of code that is used in the Sympa project should be carrefully
389              
390             The CPAN community reduce the cost of maintaining infrastructure code. And
391             by maintaining it, we mean it the great way: improve, optimize, document,
392             debug, test in a large number of perl bases, ...
393              
394             We also want to benefit as much as possible from the experience, ideas and
395             knowledge of the CPAN members.
396              
397             So if you want to contribute to Sympa, please concider picking a module on CPAN
398             that does the job and contributing to it if needed. Push your own stuff if
399             needed.
400              
401             =head2 other CPAN modules
402              
403             =head3 those we also rely on
404              
405             L for web developpement,
406             L