File Coverage

blib/lib/Sympatic.pm
Criterion Covered Total %
statement 32 36 88.8
branch 11 16 68.7
condition 2 2 100.0
subroutine 4 4 100.0
pod n/a
total 49 58 84.4


line stmt bran cond sub pod time code
1             package Sympatic;
2             our $VERSION = '0.2';
3 7     7   366738 use strict;
  7         43  
  7         212  
4 7     7   39 use warnings;
  7         11  
  7         1489  
5             require Import::Into;
6              
7             sub import {
8              
9 8     8   61 my $to; # the NS to infect
10             # Sympatic->import(@options)
11             # Sympatic->import(to => $NS, @options)
12 8 100 100     72 ('to' eq ( $_[1] // '' ))
13             ? ( $to = $_[2], splice @_,0,3 )
14             : ( $to = caller, shift );
15              
16 8         44 my %feature = qw<
17             utf8all .
18             utf8 .
19             utf8io .
20             oo .
21             class .
22             path .
23             >;
24              
25 8         50 English->import::into( $to, qw< -no_match_vars > );
26 8         22919 feature->import::into( $to, qw< say state > );
27 8         2194 strict->import::into($to);
28 8         1435 warnings->import::into($to);
29 8         1427 Function::Parameters->import::into($to);
30              
31 8         34957 while (@_) {
32              
33             # disable default features
34 3 50       24 if ( $_[0] =~ /-(?
35             utf8all |
36             utf8 |
37             utf8io |
38             oo |
39             class |
40             path
41             )/x) {
42 7     7   3197 delete $feature{ $+{feature} };
  7         2749  
  7         2041  
  3         29  
43 3         9 shift;
44 3         13 next;
45             }
46              
47             ...
48              
49 0         0 }
50              
51 8 50       33 $feature{path} and do { Path::Tiny->import::into($to) };
  8         54  
52              
53 8 100       99060 $feature{oo} and do {
54             ( $feature{class}
55 6 100       90 ? 'Moo'
56             : 'Moo::Role' )->import::into($to);
57 6         59648 MooX::LvalueAttribute->import::into($to);
58             };
59              
60 8 50       182493 $feature{utf8all} and do {
61 8         77 utf8::all->import::into($to);
62 8         359864 delete $feature{$_} for qw< utf8 utf8io >;
63             };
64              
65 8 50       46 $feature{utf8} and do {
66 0         0 utf8->import::into($to);
67 0         0 feature->import::into( $to, qw< unicode_strings > );
68             };
69              
70 8 50       104380 $feature{utf8io} and do { 'open'->import::into( $to, qw< :UTF-8 :std > ) };
  0            
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             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            
93            
94            
95              
96             Any bug report or feedback that can help to improve C 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 or even
100             L.
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 section for more details.
118              
119             =head1 DESCRIPTION
120              
121             The default behavior of L 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 project.
132              
133             Some of the recommendations are inspired by the
134             L
135             book from L (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 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 keywords
181             are exported by default.
182              
183             See the L 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
203             Documentation|http://perldoc.perl.org> (the `*tut` sections).
204             Here we provide a very short description
205              
206             The L pragma makes perl aware
207             that the code of your project is encoded in utf8.
208              
209             The L pragma avoid the
210             the perl features requiring too much caution. Also the
211             L one provides very
212             informational messages when perl detects a potential mistake. You can
213             use L to get a
214             direct reference to the perl manual when a warning or an error message is
215             raised.
216              
217             L 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
232             like the L
233             as they were abundantly criticized and will be removed in perl 5.28.
234              
235             L - enable the english (named against
236             awk variables) names for the variables documented in
237             L.
238              
239             So basically, using C, the two following instructions are the same.
240              
241             print $(;
242             print $GID;
243              
244             L introduces
245             the keywords C and C to allow function signatures with gradual typin,
246             named parameters and other features probably inspired by perl6, python and javascript.
247             See L section.
248              
249             L provides nice generic
250             way to define types that can be used from the C and C
251             signatures or the C constraint of a Moo property declaration.
252              
253             =head1 USAGE
254              
255             =head2 Declaring functions
256              
257             In addition to the C keyword provided by perl (documented in the
258             L manual), Sympatic comes with C and C
259             (provided and documented in L).
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 when you can provide a signature for a function. C provide
267             a signature syntax inspired by L 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 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 for two reasons:
309              
310             Those signatures appear as experimental in L and
311             are finally a feature in L with a changing behaviour
312             in L to make prototypes happy. Plus, we are bound
313             to L. Also, the signatures provided by
314             L) 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. 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 and L which means that
330             you can declare an object using
331              
332             =over
333              
334             =item
335              
336             C to define a new property
337              
338             =item
339              
340             C to inherit from a super class
341              
342             =item
343              
344             C to compose your class using roles
345              
346             =item
347              
348             C 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 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 helper from L
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 and the
393             L 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 on
397             L or the
398             L.
399             French people can also join us in
400             L or the
401             L.
402              
403             You are welcome to discuss about the C 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 for web development,
430             L