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, you can use perldoc, for example perldoc MDK::Common::Func will explain the functions of the Func 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.
Of course, a first look at the Perl language will be necessary for the reader. The following can be a good Perl Tutorial (though there are many of them 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.
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.
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 equivalent of chomp, but returns the chomp'ed results instead of modifying its argument.
Perl IO operations are verbose and the API is cluttered. There are many situations in which you want to read the contents of a file, put it in a scalar or loop around the files. cat_ allows to do that easily:
printf "Mandrake release:\n%s\n", cat_('/etc/mandrake-release'); foreach (cat_('/proc/mounts')) { my ($dev, $where, $type) = split; print "$dev is mounted on $where (type $type)\n"; }
Counterpart of cat_. Build the contents of the file in your program, and when you're happy with it, write it on disk:
my @resolv_conf = qw(search mandrakesoft.com); push @resolv_conf, "nameserver $_" foreach @name_servers; output('/etc/resolv.conf', @resolv_conf);
Returns true if the value is stringwise equivalent to an element of the list:
if (!member($driver, @modules)) { print "Sorry, the driver is not available in our modules.\n" }
Performs a set-wise substraction, e.g. removes in first list the elements that are members of the second one:
my @types = difference2(\@available_types, \@bad_types); print "Please select a type from: @types\n";
Removes duplicates from the list, keeping the order of the list, and the first element when duplicates.
my @types = uniq map { (split)[2] } cat_('/proc/mounts'); print "Filesystem types in use: @types\n"
Very useful for simple functional expressions.
Note: also works on lists: chomp_($a, $b) is equivalent to chomp($a); chomp($b); ($a,$b).
my $pid = chomp_(cat_('/var/run/cardmgr.pid'));
The following describes functions whose use is not trivial.
Returns LIST if the BOOL condition is true, else an empty list.
Note: it's equivalent as doing BOOL ? LIST : (), except that since it's a function, LIST is evaluated even if BOOL is false. It's useful because it's shorter and more readable than the ternary ?:.
A first convenient use is when you want to loop on a list and conditionally on another:
foreach (@types, if_($arch eq 'ppc', @ppc_types)) { # ... }
It's also useful to select elements from a list and modify them on-the-fly, e.g. performing the equivalent of a grep then a map. It works because Perl automatically concatenates lists.
my @md_devices = map { if_(/^(md\d+)/, $1) } cat_('/proc/mdstat'); # equivalent (but much more elegant!) to: my @md_devices = map { /^(md\d+)/; $1 } grep { /^md\d+/ } cat_('/proc/mdstat');
Typically used to change a parameter in a file:
substInFile { s/^FORWARD_IPV4.*\n//; $_ .= "FORWARD_IPV4=true\n" if eof } '/etc/sysconfig/network';
Useful when you need to perform an action on each element of a list, but you also need the index of each element:
each_index { printf "%s mountpoint: $_", $::i == 2 ? 'third' : 'other' } cat_('/proc/mounts');
Last update: Fri Apr 18 22:08:06 2003