Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
How to debug a Perl program

Have you every written a Perl program that doesn't perform as you would wish? Yes, everyone who's written a Perl program has done that. Once any syntax errors have been corrected, you run your program for the first time and you check - VERY carefully - the results. Any errors, and the patterns of errors, will often lead you quickly to coding errors and as you gain experience, you'll fix more and more bugs quicker and quicker this way.

But what about those 'reluctant' bugs - those things that have you scratching you head thinking "surely this cannot be happening". You need further clues, and Perl offers a number of facilities - some very simple - to help you.

1. You should run your code with the -w command line option, or with
use warnings;
specified at the top of your code. Both of these operations cause Perl to check at both compile and run time that you're not specifying something that's technically valid, but unlikely to be what you intended - it'll pick you up on uninitialised variables, variable names that only occur once, bare words that should probably have a $ in front of them, and so on.

2. The humble print statement can be very revealing - add in a few extra output statements for intermediate variables and you'll soon start to see where abouts in your code its behaviour starts to deviate from what you had intended.

3. The Data::Dumper module allows you to "pretty print" data structures if you want to go beyond the print statement - perhaps you have whole lists or hashes that you want to dump out? (Link - source example using Data::Dumper)

4. Perl comes with an interactive debugger - run Perl with the -d option if you want to run this way. There's a tutorial on this in the perl distribution - run perldoc perldebtut at the command line for more details.

If these initial four ideas don't cut it for you, there are other things to consider too.

Personally, if I'm writing a difficult piece of code I will start off with
$trace = 0;
at the top, and further down my code I'll add in statements such as
$trace and print "At point X, counter is $n, total $tot\n";
which form a report / debug facility if run; all I need to do is to switch my initial assignment to a true value, and I've turned on a tailored debugging mode. We use a similar technique on our web site pages under both PHP and Perl - where we accumulate reports using the .= operator and output them at the end of our web pages - users rarely notice and it's sometimes a huge help to us when we're looking to resolve browser specific issues or data driven problems.

Some advocates suggest that you should
use strict;
in all your code as a debug tool. I'm going to disagree. Rather, it is good practise to use the strict pragma in all .pm files that you'll be including but that should be as a matter of course and not just as a debugging aid. And I will typically omit the pragma from my main code / main program.

There's various CPAN modules to help you too - ptkdb and Devel::ebug provide a GUI to help debug, and programatic hooks for you to add your own debug facilities, if that's what you want.

And finally, there are commercial debuggers such as ActiveState's Komodo.

My own favourite toolkit - what I suggest to course delegates ...

a) Comment your code well, use sensible variable names to reduce bugs in the first place.
b) Think about your code - design it well (if informally) with appropriate UML diagrams to reduce design errors in the first place. (Link - .pdf training module on this)
c) Run with the -w command line option at least a few times.
d) use strict; within modules.
e) Add in print or Data::Dumper calls, perhaps under a $trace option, if you've got a piece of large / complex / reluctant code.

The other options? Great to have them available from time to time ...
(written 2006-06-04, updated 2006-06-05)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P711 - An Introduction to Standards in Perl
  [242] Satisfaction of training - (2005-03-11)
  [668] Python - block insets help with documentation - (2006-04-04)
  [945] Code quality counts - (2006-11-26)
  [965] KISS - one action per statement please - Perl - (2006-12-05)
  [1047] Maintainable code - some positive advice - (2007-01-21)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
  [1395] Dont just convert to Perl - re-engineer! - (2007-10-18)
  [1555] Advanced Python, Perl, PHP and Tcl training courses / classes - (2008-02-25)
  [1728] A short Perl example - (2008-07-30)
  [1853] Well structured coding in Perl - (2008-10-24)
  [1863] About dieing and exiting in Perl - (2008-11-01)
  [2375] Designing your data structures for a robust Perl application - (2009-08-25)
  [2688] Security considerations in programming - what do we teach? - (2010-03-22)
  [2875] A long day in Melksham ... - (2010-07-17)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)

P222 - Perl - Programming Efficiency and Style
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [2399] Firefighting with Perl - (2009-09-07)
  [2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
  [4611] Hungarian, Camel, Snake and Kebab - variable naming conventions - (2016-01-03)

P203 - More about the Perl Environment
  [328] Making programs easy for any user to start - (2005-05-29)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [1865] Debugging and Data::Dumper in Perl - (2008-11-02)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)

P201 - Perl - Introduction
  [25] Release numbers - (2004-08-23)
  [317] Programming languages - a comparison - (2005-05-20)
  [382] Central London Courses - Perl, PHP, Python, Tcl, MySQL - (2005-07-18)
  [577] Learning to program in Perl or PHP - (2006-01-26)
  [594] Twice is a co-incidence and three times is a pattern - (2006-02-07)
  [604] Perl - multiprocess applications - (2006-02-13)
  [629] Choosing the right language - (2006-03-01)
  [691] Testing you Perl / PHP / MySQL / Tcl knowledge - (2006-04-19)
  [846] Is Perl being replaced by PHP and Python? - (2006-08-27)
  [924] The LAMP Cookbook - Linux, Apache, MySQL, PHP / Perl - (2006-11-13)
  [1717] Q - Should I use Perl or Python? - (2008-07-23)
  [1753] Perl v PHP, choosing the right language - (2008-08-14)
  [1852] Perl and Blackberries - (2008-10-23)
  [2070] Converting to Perl - the sort of programs you will write - (2009-03-08)
  [2812] What is Perl? - (2010-06-15)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)

P050 - Perl - General
  [116] The next generation of programmer - (2004-11-13)
  [400] New in the shops - (2005-08-01)
  [1750] Glorious (?) 12th August - what a Pe(a)rl! - (2008-08-12)
  [1897] Keeping on an even keel - (2008-11-21)
  [2228] Where do I start when writing a program? - (2009-06-11)
  [2242] So what is this thing called Perl that I keep harping on about? - (2009-06-15)
  [2374] Lead characters on Perl variable names - (2009-08-24)
  [2504] Learning to program in ... - (2009-11-15)
  [2736] Perl Course FAQ - (2010-04-23)
  [2783] The Perl Survey - (2010-05-27)
  [2825] Perl course - is it tailored to Linux, or Microsoft Windows? - (2010-06-25)
  [2971] Should the public sector compete with businesses? and other deep questions - (2010-09-26)
  [3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
  [3322] How much has Perl (and other languages) changed? - (2011-06-10)
  [3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
  [3407] Perl - a quick reminder and revision. Test yourself! - (2011-08-26)
  [3823] Know Python or PHP? Want to learn Perl too? - (2012-07-31)
  [3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
  [3911] How well do you know Perl and / or Python? - (2012-11-04)
  [4296] Polishing the Perl courses - updated training - (2014-09-17)
  [4301] Perl - still a very effective language indeed for extracting and reporting - (2014-09-20)


Back to
A visit from the solicitor
Previous and next
or
Horse's mouth home
Forward to
We can offer a room, but we can't operate on a dog
Some other Articles
The Fag Packet Design Methodology
Domain Listing Center and Domain Registry of America
Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus.
We can offer a room, but we can't operate on a dog
How to debug a Perl program
A visit from the solicitor
Last week - picture of the Perl course
Finishing up in Dhahran
The eye
(Perl) Callbacks - what are they?
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/743_How- ... ogram.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb