perl-MDK-Common tutorial v0.1

Guillaume Cottenceau


Introduction

This document aims at helping people interested in learning more on perl-MDK-Common, a Perl library which is intensively used in MandrakeSoft in-house software development.

The library adds some convenient "basic" functions to Perl, allows easier functional-style programming, and also provides some better system-related operations. It can be seen as an extension to the standard Perl library, adding missing helpful functions. It is divided as follows:

Thanks to perl-MDK-Common's own documentation, an easy way to directly access information about the provided functions is to use perldoc. For example, perldoc MDK::Common::Func will list the functions of the Func sub-module. Use perldoc MDK::Common to view information on all the available functions.

Additionally, perl-MDK-Common provides a binary called perl_checker, which is a Perl compiler aiming at enforcing the use of a subset of Perl, so that all MandrakeSoft Perl programs roughly follow the same code style. It will also help the programmer to remove unneeded parentheses and conditionals.


Prerequisites

Of course, a first look at the Perl language will be necessary for the reader. The following can be a good Perl Tutorial (although there are many others on the web): http://www.comp.leeds.ac.uk/Perl/.

Programming with perl-MDK-Common also emphasizes the following quality properties on your code:

We can't discuss Perl programming without referring to two excellent books from O'Reilly. The first one is called "The Perl Cookbook", and covers many daily problems a Perl programmer will face, in a recipe-like fashion. All Perl programmers should own this book :). The second one can be a good resource for more skillful programmers, and is called "Advanced Perl Programming"; it covers interesting advanced features of Perl.


Structure of this document

This document will first try to emphasize the most useful functions of the perl-MDK-Common library, e.g. the most commonly used and simple. Then, some functions whose use is not trivial will be explained. As a last part, an introduction to the code-style to please perl_checker will be shown.


Most useful functions

Note: many functions' name, extending capabilities of existing functions, or being their functional counterpart, are suffixed with the underscore character (_); for example, chomp_ is the semantical equivalent of chomp, but returns the chomp'ed results instead of modifying its argument.


Other interesting functions

The following describes functions whose use is not trivial.


perl_checker

Let's examine now the code-style perl_checker wants you to adopt. Let's consider the following naive code example:

  1: sub calc {
  2:     my ($x,$y) = @_;
  3:     $_ = $y;
  4:     ($x==0 && $y==0) and return -1;
  5:     my @tab = (1, 2, 3);
  6: 			
  7:     /sysconfig\/i18n/ and return 1;
  8: }
  

The following problems are reported:

Finally, the correct code looks like:

  sub calc {
      my ($x, $y) = @_;
      local $_ = $y;
      $x == 0 && $y == 0 and return -1;
      my @_tab = (1, 2, 3);
  			
      m|sysconfig/i18n| and return 1;
  }
  

Under Emacs, you might want to add the following to your .emacs and then be able to validate your code with C-Enter:

  (defmacro ilam (&rest body) `(lambda () (interactive) ,@body))
  (add-hook 'cperl-mode-hook 
	    '(lambda ()
	       (local-set-key [(control return)] 
			      (ilam (save-some-buffers 1) (compile (concat "perl_checker --restrict-to-files " (buffer-file-name (current-buffer))))))
	       ))
  

Last update: Wed Apr 30 18:05:40 2003