From b1f265fec66a76739369eeff9c0c7edd8539ea40 Mon Sep 17 00:00:00 2001
From: Guillaume Cottenceau 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
+ 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
@@ -64,7 +64,7 @@
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
@@ -84,7 +84,7 @@
common technique among programmers, and maybe it's even worse
with Perl programmers; but functional-style programs are often
clearer, more expressive, more reusable, and more
- maintainable
+ maintainable, than traditional programs
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.
Counterpart of cat_. Build the contents of the file - in your program, and when you're happy with it, write it on - disk:
+Counterpart of cat_:
- my @resolv_conf = qw(search mandrakesoft.com); - push @resolv_conf, "nameserver $_" foreach @name_servers; - output('/etc/resolv.conf', @resolv_conf); + output('/tmp/resolv.conf', + "search $domain\n", + map { "nameserver $_\n" } @name_servers);
Useful when you need to perform an action on each element of a list, but you also need the index of each element:
@@ -313,13 +312,115 @@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: +
+ ++line 2, character 12-12 +you should have a space here+ +
+ Good: my ($x, $y) = @_;
+
+ Why: you should put a space after the comma when specifying a list.
+
+line 3, character 5-7 +undeclared variable $_+ +
+ Good: local $_ = $y;
+
+ Why: you should always localize $_ when you set it, because it's a global variable.
+
+line 4, character 8-8 +you should have a space here+ +
+ Good: ($x == 0 && $y == 0) and return -1;
+
+ Why: you should put spaces before and after operators.
+
+line 4, character 5-21 +unneeded parentheses+ +
+ Good: $x == 0 && $y == 0 and return -1;
+
+ Why: because of operators precedence, the parentheses are unneeded (if unsure about precedence, see perlop(1))
+
+line 5, character 8-12 +unused variable @tab+ +
+ Why: Assigning to unused variables is (typically) useless. If you really need to assign to an unused variable, prefix its name with `_' and perl_checker will stop boring you (for example, @_tab). +
+ ++line 7, character 20-21 +change the delimit character / to get rid of this escape+ +
+ Good: m|sysconfig/i18n|
+
+ Why: / is not the only regexp delimiter! if you want to specify a slash in your regexp, use another delimiter so that your regexp will be more readable.
+
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: Fri Apr 18 22:08:06 2003 + Last update: Wed Apr 30 18:05:40 2003