X hits on this document

PDF document

Running and Debugging Perl - page 27 / 30

91 views

0 shares

0 downloads

0 comments

27 / 30

Running and Debugging Perl

Summary

Whenever you program, you'll inevitably make mistakes and create bugs. There are two types of bug you'll come up against: the syntax error, which manifests itself with a violent bang, and the logic error, which hides away insidiously inside your program and drives you silently mad. This chapter has shown you how you can deal with both sorts of bug.

We've looked at Perl's error messages and the most common causes of syntax errors. We've seen how to decode the error messages perl gives, both by employing a little bit of logical thought (the best way) and by getting the diagnostics pragma to explain it to us (the easiest way).

We've also seen how to avoid creating bugs in the first place – use warnings and use strict act as checks to ensure that we're not doing anything too crazy. There are also plenty of ways to use defensive programming, imposing further checks to stop bugs before they happen.

Perl is a great tool for use on the command line. I'm forever using it to search files for patterns and change files with a search-and-replace, as well as using it to test out snippets of Perl code and examine Perl's behavior. We've looked at various command line switches, which make it easy for us to do complex things: loop over a file, change a file in place, check the syntax of a file or piece of code, and so on.

We've also doffed our cap to the Perl debugger, as well as some other ways to detect and remove bugs in our code. Now you're armed to do battle with any bugs that come your way – and come they will!

Exercises

Take a look at the following file, apply what you've read about, and see if you can knock it into shape:

#!/usr/bin/perl #buggy.plx

my %hash;

until (/^q/i)

{

print "What

would

you

like

to

do?

('o'

for

options):

"

$

=

STDIN;

if

($

eq

"o"){options}elsif($

eq "r"){read}elsif($

eq "l"){ list }elsif

($ eq

"w"){

else {

print

write }elsif ($

eq "d")

"Sorry,

not

a

recognized

{ delete } elsif option.\n"; }

($

eq "x")

{ clear }

sub options { print<<E0F Options available:

  • o

    - view options

r - read entry l - list all entries w - write entry

305

Document info
Document views91
Page views91
Page last viewedSat Dec 10 21:09:07 UTC 2016
Pages30
Paragraphs723
Words10014

Comments