UPDATE: I've mostly left this Meditation as it was 15 years ago, but since then it's also appeared in Mastering Perl and I'm updating it at briandfoy.github.io.
brian's Guide to Solving Any Perl Problem
Follow this guide and save your sanity
I believe in three things:
Forget about how you do things. If the way you did things worked, you would not be reading this. That is not a bad thing. It is just time to evolve. We have all been there.
You can turn on strictures within the code with the strict pragma.
use strict;
You can turn on strictures from the command line with perl's -M switch.
perl -Mstrict script.pl
You may be annoyed at strictures, but after a couple of weeks of programming with them turned on, you will write better code, spend less time chasing simple errors, and probably will not need this guide.
You can use perl's -w switch in the shebang line.
#!/usr/bin/perl -w
You can turn on warnings from the command line.
perl -w script.pl
You can use lexical warnings with all sorts of interesting features. See warnings for the details.
use warnings;
If you do not understand a warning, you can look up a verbose version of the warning in perldiag or you can use the diagnostics pragma in your code.
use diagnostics;
print STDERR "The value is ]$value]\n";
I enclose $value in braces so I can see any leading or trailing whitespace or newlines.
If I have anything other than a scalar, I use Data::Dumper to print the data structures.
require Data::Dumper;
print STDERR "The hash is ", Data::Dumper::Dumper( \%hash ), "\n";If the value is not what you think it is, back up a few steps and try again! Do this until you find the point at which the value stops being what you think it should be!
You can also use the built-in perl debugger with perl's -d switch. See perldebug for details.
perl -d script.pl
You can also use other debuggers or development environments, like a ptkdb (a graphical debugger based on Tk), Komodo (ActiveState's Perl IDE based on Mozilla), of Affrus on MacOS X.
You can look up a particular function with the perldoc command and its -f switch.
perldoc -f function_name
If you are using a module, check the documentation to make sure you are using it in the right way. You can check the documentation for the module using perldoc.
perldoc Module::Name
Again, I constantly refer to perlvar. Well, not really since I find The Perl Pocket Reference much easier to use.
perl -MModule::Name -le 'print Module::Name->VERSION';
If you read most of your documentation off of the local machine, like at http://www.perldoc.com or http://search.cpan.org, then you are more likely to encounter version differences in documentation.
Perl stores the environment in %ENV. If you need one of those variables, be ready to supply a default value if it does not exist, even if only for testing.
If you still have trouble, inspect the environment.
require Data::Dumper; print STDERR Data::Dumper::Dumper( \%ENV );
If you don't have a test suite, why not make one? If you have a really small script, or this is a one-off script, then I will not make you write a couple of tests. Anything other than that could really benefit from some test scripts. Test::Harness makes this so simple that you really have no excuse not to do it. If you do not have the time, perhaps you are wasting too much time debugging scripts without tests. MakeMaker is just not for modules after all.
For a couple of years I had the pleasure of working with a really good programmer who could solve almost anything. When I got really stuck I would walk over to his desk and start to explain my problem. Usually I didn't make it past the third sentence without saying ``Never mind---I got it''. He almost never missed either.
Since you will probably need to do this so much, I recommend some sort of plush toy to act as your Perl therapist so you do not annoy your colleagues. I have a small bear that sits on my desk and I explain problems to him..
You have been staring at the computer screen, so maybe a different medium will let you look at things in a new way. Try looking at a print-out of your program.
If you don't want to use dead trees, try looking at the source without syntax highlight, or even in a different editor. Change up the way it looks and the fonts you use so you see different patterns.
In reply to brian's Guide to Solving Any Perl Problem by brian_d_foy
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |