The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Sympatic - A more producive perl thanks to CPAN

STATUS

SYNOPSIS

    package Counter;
    use Sympatic;

    use Types::Standard qw< Int >;

    has qw( value is rw )
    , default => 0
    , lvalue  => 1
    , isa     => Int;

    method next { ++$self->value }
    method ( Int $add ) { $self->value += $add }

see usage section for more details.

DESCRIPTION

This document is an introduction to Sympatic Perl,

project developers community focus on

The default behavior of Perl could be significantly improved by the pragmas and CPAN modules so it can fit the expectations of a community of developers and help them to enforce what they consider as the best practices. For decades, the minimal boilerplate seems to be

    use strict;
    use warnings;

This boilerplate can evolve over the time be much more larger. Fortunately, it can be embedded into a module.

Sympatic is the boilerplate module for the Sympa project project.

Some of the recommendations are inspired by the Perl Good Practices book from Damian Conway. We refer to this book as PBP in this document.

the goals behind Sympatic

This section describes the goals that leads to the choices made for Sympatic and the coding style recommendations.

balance between old servers and modern tools

As we try to keep noone left behind, we also need to think about the future.

As some sympa servers run on quiet old unix systems, we try to make our code running on old versions of the perl interpreters. However, this should not take us away from features of recent versions of perl that really helps performances, stability or good coding practices.

We are currently supporting all the versions of perl since perl 5.16 (released the 2012-May-2). That's the best we can afford. Please contact us if you need support of older Perl.

reduce infrastructure code

As perl emphasize freedom, it leaves you on your own with minimal tooling to write such simple and standard things most of us don't want to write by hand anymore (like object properties getters and setters, function parameter checkings, ...). This code is described by Damian Conway as "the infrastructure code".

CPAN provide very good modules to make those disapear and we picked the ones we think to be the most relevant. Putting them all together provides the ability to write very expressive code without sacrifying the power of Perl.

make perl more familiar for newcommers

Choosing the CPAN modules to reduce infrastructure codes and writing the coding style recommendation below was made with our friends from the other dynamic langages in mind. We really expect developpers from the ruby, javascript and python provide a much better experience using Sympatic as it provides some idioms close to the ones they know in adition of the unique perl features.

less typing and opt out policy

Sympatic has the ability to provide different sets of features (see features section) and the ones that are imported by default are the one that are used in the most common cases. For exemple: as most of the sympa packages actually are objects, the Moo keywords are exported by default.

See the features section to learn how to avoid some of them.

what use Sympatic means?

If you are experimented Perl developpers, the simplest way to introduce Sympatic is to say that

    use Sympatic;

is equivalent to

    use strict;
    use warnings;
    use feature qw< unicode_strings say state >;
    use English qw< -no_match_vars >;
    use utf8;
    use Function::Parameters;
    use Moo;

If you're not, we highly recommend the the well written Perl Documentation (the `*tut` sections). Here we provide a very short description

The utf8 pragma makes perl aware that the code of your project is encoded in utf8.

The strict pragma avoid the the perl features requiring too much caution. Also the warnings one provides very informational messages when perl detects a potential mistake. You can use diagnostics to get a direct reference to the perl manual when a warning or an error message is risen.

feature is the Perl pragma to enable new features from new versions of the perl interpreter. If the perl interpreter you are using is too old, you will get an explicit message about the missing feature. Note that we use

    use feature qw< unicode_strings say state >;
    use strict;
    use warnings;

instead of

    use v5.14;

to avoid the use of features related to smart match like the given/when flow control as they were abundantly criticized and will be removed in perl 5.28.

English - enable the english (named against awk variables) names for the variables documented in the perlvar manual.

so basically, using Sympatic, the two following instructions are the same.

    print $(;
    print $GID;

Function::Parameters introduce the keywords fun and method to allow function signatures with gradual typin, named parameters and other features probably inspired by perl6, python and javascript. See examples section.

Type::Standard provide nice generic way to define types that can be installed used from the fun and method signatures or the isa contraint of a Moo property declaration.

USAGE

declaring functions

In addition to the sub keyword provided by perl (documented in the perlsub manual), Sympatic comes with fun and method (provided and documented in Function::Parameters).

As those two documentations are very well written, the current documentation only discuss about picking one of them and providing some exemples.

Use fun when you can provide a signature for a function. fun provide a signature syntax inspired by perl6 so you can use positional and named parameters, default values, parameter destructuring and gradual typing. You should use it in most cases.

Here are some examples:

    # positional parameter $x
    fun absolute ( $x ) { $x < 0 ? -$x : $x }

    # typing
    use Types::Standard qw< Int >;
    fun absolute ( Int $x ) { $x < 0 ? -$x : $x }

    # default parameters
    fun point ( $x = 0, $y = 0 ) { "( $x ; $y )" }
    point 12; # ( 12 ; 0 )

    # named parameters
    fun point3D ( :$x = 0, :$y = 0, :$z = 0 ) { "( $x ; $y ; $z )" }
    say point3D x => 12; # ( 12 ; 0 ; 0 )

Use the sub keyword fully variadic functions (the parameters are stored in the @_ array) or to use for example, let's assume you want to write a simple CSV seriralizer usable like this

    print csv qw( header1 header2 header3 );
    # outputs:
    # header1;header2;header3

this is a naive implementation demonstrating the use of @_

    sub csv { ( join ';', @_ ) , "\n" }

common cases are list reduction or partial application

default perl signatures, prototypes and attributes

Experienced perl programmers should note that note that we don't use the perl signatures as documented in perlsub for two reasons:

Those signatures are appears as experimental in perl5.20 and are finally perl5.26 (as we are bound to perl5.16). Also, the signatures provided by Function::Parameters) are much more powerful than the core ones (see description above).

Attributes are still available. If you need to declare a prototype, they are available using the :prototype() attribute as described in the OMGTODOFINDALINK. For exemple

    fun twice ( $block ) :prototype(&) { &$block; &$block }
    twice {say "hello"}
    # outputs:
    # hello
    # hello

Object Oriented programming

Sympatic imports Moo and Function::Parameters which means that you can declare an object using

  • has to define a new property

  • extends to inherit from a super class

  • with to compose your class using roles

  • method to combine with roles

TODO: that keywords like around, after ?

    use Sympatic;
    use Type::Standard qw< Int >;

    has value
        ( is      => 'rw'
        , isa     => Int
        , lvalue  => 1
        , default => 0 );

    method add ( Int $x ) { $self->value += $x }

Note that the method add() is almost useless when $self-&gt;value is lvalue.

    package Human;
    use Sympatic;
    use Type::Standard qw< InstanceOf Str >;

    has qw( name is rw )
    , isa  => Str;

    method greetings ( (InstanceOf['Human']) $other ) {
        sprintf "hello %s, i'm %s and i want to be a friend of you"
            , $self->name
            , $other->name
    }

work with the filesystem

the "all in one" path helper from Path::Tiny is exported by Sympatic. refer to the documentation for examples.

set/unset features

    TODO: describe how to enable/disable features
    TODO: describe the features themselves

CONTRIBUTE

you are welcome to discuss about the Sympatic style on the Sympa project developpers mailing list. If your proposal is accepted, edit the module the way you describe, update the documentation and test the whole thing.

    cpanm --installdeps .
    RELEASE_TESTING=1 prove -Ilib t

Sympa and CPAN

Every line of code that is used in the Sympa project should be carrefully

The CPAN community reduce the cost of maintaining infrastructure code. And by maintaining it, we mean it the great way: improve, optimize, document, debug, test in a large number of perl bases, ...

We also want to benefit as much as possible from the experience, ideas and knowledge of the CPAN members.

So if you want to contribute to Sympa, please concider picking a module on CPAN that does the job and contributing to it if needed. Push your own stuff if needed.

other CPAN modules

those we also rely on

Dancer2 for web developpement, Template Toolkit for text templating,

those which can be useful too

Curry provide ease the creation of streams and callbacks.

    sub { $self->foo('bar') }

can be written as

    $self->curry::foo('bar')

Perlude is the way to manipulate and combine streams.

AUTHORS

thanks for those people who contributed to the sympatic module (by date)

Marc Chantreux
David Verdin
Mohammad S Anwar
Stefan Hornburg (Racke)

LICENSE AND COPYRIGHT

Copyright 2018 Sympa community <sympa-developpers@listes.renater.fr>

This package is free software and is provided "as is" without express or implied warranty. It may be used, redistributed and/or modified under the terms of the Perl Artistic License subee (http://www.perl.com/perl/misc/Artistic.html)

LICENCE

    Copyright (C) 2017,2018 Sympa Community

    Sympatic is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    Sympatic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, see <http://www.gnu.org/licenses/>.

    Authors:
    Marc Chantreux <marc.chantreux@renater.fr>
    Stefan Hornburg (Racke) <racke@linuxia.de>