Howto write good perl programs

(Or at least not making the same mistakes again and again).

Avoid creating circular memory references at all costs

Use modules like Devel::Cycle to check for circular references. And never do stuff like:

	$this->{stuff} = $this;
	

Bad examples of code that does this are Mail::SpamAssassin, Mail::Audit (now unmaintained), etc.

Try to use the /o modifier in regular expressions as much as possible.

So the regex is only compiled once. This can't be done when the regex depends on a variable, like in:
m/some stuff \d $text/
So it's better to make regex static whenever possible. Even if you have to check against a group of regexps.

Filenames end at the end of string, not in newline

This is bad: $filename =~ m/^stuff this and that$/o # check if filename is "stuff this and that". *WRONG*
Filename could be: "stuff this and that\n" and "stuff this and that". Newline is a valid character in filenames.

While this is good: $filename =~ m/\Astuff this and that\z/o

Also filenames can have spaces and regexp metacharacters, so if you have the filename stored in a variable and want to check that in a regex, disable metacharacters before the variable read on.

Be careful with regexp interpolation

As I was saying previously, a filename like:
$fname = "stuff priced at 50.xml"; When used in a regex like:
$file =~ m/\A$fname\z/;
Would match "stuff priced at 509xml" and "stuff priced at 50-xml", so you have to do it like:
$file =~ m/\A\Q$fname\E\z/

Use "strict" and "warnings" when your program grows more than 25 lines

Use it early and keep your code clean. Reduce the use of global variables.

Valid HTML 4.01! Valid CSS!