HTML CSS PHP PERL

Exercise: Finding the Bug

 
Previous Table of Contents Next

Exercise: Finding the Bug

This exercise shows you how to use the debugger. The program in Listing 12.1 has a problemactually two. It's supposed to print these messages:


20 glasses of Lemonade on the wall

19 glasses of Lemonade on the wall

:

1 glass of Lemonade on the wall

0 glasses of Lemonade on the wall


But it does not. Your task is to type the program in Listing 12.1 and try to find the bugs. Neither of the bugs is a syntax problemPerl's warnings are not triggered, and use strict prints no messagesbut the debugger should make the bugs fairly obvious to find.

After you type the program, run Perl with the debugger to try to find the errors. Remember to print relevant variables and expressions occasionally and step through the function calls one at a time.

Listing 12.1. Buggy

1:   # !/usr/bin/perl -w

2:   # This program contains TWO errors

3:   use strict;

4:

5:   sub message {

6:      my($quant)=@_;

7:      my $mess;

8:      $mess="$quant glasses of Lemonade on the wall\n";

9:      if ($quant eq 1) {

10:             $mess=s/glasses/glass/;

11:      }

12:      print $mess;

13:  }

14:

15:  foreach(20..0) {

16:     &message($_);

17:  }


The solution is presented in the "Quiz" section at the end of this hour.

    Previous Table of Contents Next
    © 2000- NIV