Basic rules of decent Perl programming

3 04 2008
Basic rules of decent Perl programming
* Use the -w switch on the she-bang line. When using this switch, Perl
will complain about variables only used once, undefined filehandles
etc. Read the perlrun manual page for further information. We can
consider the -w switch as Golden Rule #1.* “use strict;” The strict pragma will enforce you to write more
bullet-proof programs. You have to predeclare your variables, avoid
bare-words etc. More on it in perldoc strict. “use strict;” is Golden
Rule #2.* Check the return value of system calls. Inform the user, what went
wrong; you’ll also save a lot of time when debugging. The *$!*
variable contains the last system call error. Use clear and
informative error messages, e. g. if you are trying to open a file,
the error message should print out something like: “Cannot open
‘$filename’ for reading: $!”. This is Golden Rule #3.

* Always use the CGI module when writing a CGI script. There is more
than one way to do it, but not without using CGI.pm.

* Use -T in your CGI scripts. Do not trust any user input (e.g. web
forms etc). The perlsec pages will help you on your way.

* Read perldoc perldiag. Optionally you can use the diagnostics pragma
in your programs. It provides you even more debug messages than -w,
and it will explain the errors, you get.

* You may use the Fatal module. It will replace functions with
equivalents which succeed or die. See the manual for details.

* Use existing modules from CPAN. Don’t try to reinvent the wheel,
unless you reinvent a rounder one. You can download modules at
http://www.cpan.org/“, and you can search for modules at
http://search.cpan.org/“.

* Use Perl’s internal debugger. It is intended to help you. Read
perldoc perldebug and save several hours and a lot of headache.

* Make your source code easily readable. Clarity is a measurement of a
good Perl program. Comment your code: after a year or two, you won’t
remember what did you do and why. Comments also help others to
understand and maintain your code. Read the perlstyle pages.

* Use Data::Dumper for printing complex data structures.

* Use lexical scoping whenever possible. Don’t mess the global variable
space. If you accidentally use the same name elsewhere, you destroy
existing values.

* Read the perltrap pages. It’s necessary if you are coming from the
AWK/sed/C world.

* Take the advice of experienced Perl coders.

* Always read the fine manuals. And remember: there is more than one
way to do it.
AUTHOR

(C)opyright: Peter BARABAS , 2002. All
(R)ights reserved.


Actions

Information

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>