Using the Perl Debugger

One of the more useful tools when it comes to exploring a language is the debugger. This tool allows you to see real-world programs in action and examine what they are doing.

First you need a Perl script. Listing 1.1 shows a program that computes the average grade for a list of students.

Listing 1.1. ave.pl (Line numbers added)
 1  use strict; 
 2  use warnings; 
 3 
 4  while (<DATA>) 
 5  {
 6      if (/^#/) 
 7      {
 8          next; 
 9      } 
10      my @grades = split /[ \t]+/, $_; 
11      my $name = shift @grades; 
12      my $ave = ($grades[0] + $grades[1] + $grades[2]) / 3; 
13      print "Student $name Average $ave\n"; 
14  } 
15 
16  __DATA__ 
17  # Student       Grade   Grade   Grade 
18  oualline        98      95      92 
19  smith           75      84      99 
20  jones           45      26      55

Spelling __DATA__

__DATA__ is spelled “<Underscore><Underscore>DATA<Underscore><Underscore>” ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.