background preloader

Better Code

Facebook Twitter

Les tests en Perl - Présentation et modules standards. Perl dispose depuis longtemps (Perl 1 l'utilisait déjà) d'une technique de test assez simple mais assez efficace. Elle consiste à exécuter des scripts qui vont réaliser des suites de tests basiques pour vérifier le bon fonctionnement du programme testé. Chaque test signale s'il s'est bien exécuté ou pas en affichant sur la sortie standard "ok" ou "not ok". Cela fonctionne donc sur un principe similaire aux checklists effectuées par les ingénieurs de l'ESA ou de la NASA (où Larry Wall a d'ailleurs travaillé). Illustrons avec un exemple trivial : $a = 1 + 1; print $a == 2 ? Et pour connaître l'avancement des tests en cours, il suffit que le script de test affiche avant cela le nombre total de tests qu'il compte exécuter : print "1..2\n"; # pour exécuter deux tests Cela permet de savoir où on en est et de vérifier si, à la fin du script, on a bien exécuté le nombre de tests annoncés.

Perl Testing: A Developer's Notebook Séance de dissection Le plan est la première ligne attendue. Bail out! Critic - Automatically review your co. [ Perl tips index ] [ Subscribe to Perl tips ] The best way to do things in Perl keeps changing, just as the language and the art of programming does.

Critic - Automatically review your co

The code you wrote 10 years ago might have been best practice back then, but chances are it doesn't look too good by modern standards. Ten Essential Development Practices. The following ten tips come from Perl Best Practices, a new book of Perl coding and development guidelines by Damian Conway. 1.

Ten Essential Development Practices

Design the Module's Interface First The most important aspect of any module is not how it implements the facilities it provides, but the way in which it provides those facilities in the first place. If the module's API is too awkward, or too complex, or too extensive, or too fragmented, or even just poorly named, developers will avoid using it. They'll write their own code instead. Designing module interfaces requires both experience and creativity. The key, however, is to write that code as if the module were already available, and write it the way you'd most like the module to work. Once you have some idea of the interface you want to create, convert your "play tests" into actual tests (see Tip #2). 2.

Probably the single best practice in all of software development is writing your test suite first. Write the tests first. 3. 4. 5. > substrate --help 6. 7. Tidy - Parses and beautifies perl source. Perl::Tidy - Parses and beautifies perl source use Perl::Tidy; Perl::Tidy::perltidy( source => $source, destination => $destination, stderr => $stderr, argv => $argv, perltidyrc => $perltidyrc, logfile => $logfile, errorfile => $errorfile, formatter => $formatter, dump_options => $dump_options, dump_options_type => $dump_options_type, ); This module makes the functionality of the perltidy utility available to perl scripts.

Tidy - Parses and beautifies perl source

Any or all of the input parameters may be omitted, in which case the @ARGV array will be used to provide input parameters as described in the perltidy(1) man page. For example, the perltidy script is basically just this: use Perl::Tidy; Perl::Tidy::perltidy(); The module accepts input and output streams by a variety of methods.

The following chart illustrates the logic used to decide how to treat a parameter. If the parameter is an object, and the object has a close method, that close method will be called at the end of the stream. Unraveling Code with the Debugger. Many people who work with Perl code never touch the debugger.

Unraveling Code with the Debugger

My goal in this article is to provide reasoned argument for adding the Perl debugger to your set of tools, as well as pointers on how to do so. Many people are most comfortable with adding debugging variables and print statements to their code. These are fine techniques; I use them too, when they are appropriate. At other times, the debugger has saved me from tearing my hair out. To mangle an old saying, with apologies to Sartre, "Hell is other people's code. " This article is a case example of using the Perl debugger in a production environment. I've written the article so that you can follow along yourself, if you wish. Additionally, for any use of the debugger, you should install the Perl modules Term::ReadLine and Term::ReadKey from CPAN. Statement of the Problem. Size - Memory usage of variables. Devel::Size - Perl extension for finding the memory usage of Perl variables use Devel::Size qw(size total_size); my $size = size("A string"); my @foo = (1, 2, 3, 4, 5); my $other_size = size(\@foo); my $foo = {a => [1, 2, 3], b => {a => [1, 3, 4]} }; my $total_size = total_size($foo); This module figures out the real size of Perl variables in bytes, as accurately as possible.

Size - Memory usage of variables

Call functions with a reference to the variable you want the size of. If the variable is a plain scalar it returns the size of this scalar. If the variable is a hash or an array, use a reference when calling.