Workshop

The workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.

Quiz

1:What is a debugger for? Why is it more useful than, say, -w or print?
A1: The -w command in Perl is there to help you fix (or avoid) syntax mistakes or poor-coding practices. The Perl debugger is there to help you with all other problems: arrays not getting built or not getting printed, values not getting matched, script logic not running the way you expect it to. Using print statements will produce some of the same effect, but it'll take longer and often be more difficult to figure out the problem. With the debugger you can also change values of variables and execute different bits of code at different times while your script is running—something you cannot do with a simple print statement.
2:How do you start the Perl debugger?
A2: Start the Perl debugger by running perl on the command line with the -d option followed by the name of your script and any script arguments. In MacPerl, choose Perl Debugger from the Script menu.
3:Name three ways of listing bits of code inside the debugger.
A3: There are several ways of listing code in the debugger:
  • Use the l command to show succeeding chunks of code

  • Use l with a line number to show that line number

  • Use l with a range of line numbers to show those lines

  • Use - to show the preceding chunks of code

  • Use w to show a few lines before and a few lines after the current line

4:How do you print the values of variables in the debugger?
A4: Print values of global and package variables using the X command with a variable name (minus the $, @, or %). Use the x command to print other variables and execute other Perl expressions (such as $hash{'key'}).
5:How is tracing execution different from stepping through each line of code?
A5: Turning on tracing shows each line of code as it's being executed, whether you are stepping through the execution or not. Stepping through the code shows each line as it's about to be executed. Stepping through code using the n command skips over subroutines (they get executed, but their contents are not displayed).
6:How are the X and V commands for printing variables different?
A6: The X command prints the variables for the current package (main). The V command prints variables in any given package (something you'll need later when you work with packages).

Exercises

Type the following script:

#!/usr/local/bin/perl -w

my @foo = (2,5,3,7,4,3,4,3,2,3,9);

foreach $thing (0..10) {
    &timesit($thing, $foo[$thing]);
}

sub timesit {
    my ($num, $val) = @_;
    print "$num times $val is ", $num * $val, "
";
}

Run the debugger on it. Perform the following debugger operations:

1:Turn on tracing using the t command. Type c to see the result.
A1: 1–4. There are no answers to these exercises; they demonstrate the use of the debugger.
2:Type R to restart running the script. Use n to step through the script. When you get inside the foreach loop, print the values of $thing a number of times.
3:Type R to restart the script again. Use s to step through the script. Print the values of $num and $val from inside the &timesit() subroutine. Use r to return from inside the &timesit() subroutine.
4:Type R to restart the script. Set a breakpoint at the &timesit() subroutine using the b command. Type L to view the breakpoint. Type c to run until the breakpoint is hit. Use d to delete the breakpoint.
5:BUG BUSTER: Use the debugger to find the bugs in this script:
#!/usr/local/bin/perl -w

@foo = (2,5,3,7,4,3,4,3,2,9);

while ($i < $#foo) {
    &timesit($i, $foo[$i]);
}

sub timesit {
    my ($num, $val) = @_;
    print "$num times $val is ", $num * $val, "
";
}
									

A5: There are two bugs in the script:
  • $i is never initialized, which means that the &timesit() subroutine will get initialization errors when it tries to print the value of $num.

  • $i is never incremented, which means it will be 0 each time and go into an infinite loop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset