When your program misbehave, goes nuts or fails, very often, it is because your variable don't contain what you think. One basic debugging technique is to take the habit to print the contents of your variables.

For this you can use a debugger, but there are some other very basic ways.

To really know the content of a variable (say $foo) just add a print statement like this to you code:

Code

print "<$foo>";

This way, if your program prints:

Code

<bar > # Or < bar>

you know that you have an unexpected trailing end of line character or leading white space.

For variable containing more complicated things such as arrays, hashes of other data structure, use the Data::Dumber module (which is core, so should be available on any Perl install).