next up previous contents
Next: Examples: Using the n Up: Stepping Through Your Script Previous: Stepping Through Your Script

Displaying Information

The debugger uses the concept of a current display line. The current display line is simply the last line that has been displayed by the l command. When the debugger first starts, the current display line is the first executable line. Here are some examples.

01: package Inventory_item;

02:    sub new {

03:    }

04:

05: package Pen;

06:     @ISA = (Inventory_item);

07:

08:     sub new {

09:     }

10:

11: package Color;

12:     print("Executing Color statements\n");

13:     $colors{"blue"}  = "Die Lot 13";

14:     $colors{"red"}   = "Die Lot 5";

15:

16:     sub new {

17:     }

18:

19: package main;

20:     print("Executing main statements\n");

Note This listing is identical to Listing 14.5 except that the guts of the functions have been removed. This was done simply to shorten the listing.

If you load this script into the debugger (perl -d 16lst01.pl), you will see that the first displayed line is line 6. The lines before line 6 are package and function statements. Line 6 will also be the current execution line.

If you issue the l debugger command, lines 6 to 15 are displayed:

6:          @ISA = (Inventory_item);

7:

8:          sub new {

9:          }

10:

11:     package Color;

12:         print("Executing Color statements\n");

13:         $colors{"blue"}  = "Die Lot 13";

14:         $colors{"red"}   = "Die Lot 5";

15:

After this display, the current display line is changed to 15, but the current execution line is still line 6. If you issue the l debugger command again, lines 16 to 20 are displayed.

You can display the first line of your script by using the l 1 debugger command. This command displays the first line of the script and changes the current display line:

1: package Inventory_item;

Because this script uses package names to change the namespace in which the functions are defined, simply issuing l new does not display a new() function. Instead, you need to use the double-colon (::) notation to specify which namespace to use. For example, l Color::new displays:

16:         sub new {

17:         }

While inside the debugger, you can use the X and V commands to view variables. These commands are very good for simple variables, but I have not found them to be useful for complex data structures. For example, consider a small program that creates an array within an array data structure, debug5.pl.

sub prtArray {

    my(@array)      = @_;

    my($index)      = 0;

    foreach (@array) {

        if (ref($_) eq 'ARRAY') {

            my($innerIndex) = 0;

            foreach (@{$array[3]}) {

                print("\t$innerIndex\t'$_'\n");

                $innerIndex++;
            }
        }

        else {

            print("$index\t'$array[$index]'\n");
        }
        $index++;
    }
}

@array = (1, 2, 3, [1, 2, 3], 4);    # an array inside an array.

1;

Note This listing is for illustrative purposes only. The crude method used to print the data structure is not recommended for practical use. I suggest that you invest time creating a general-use routine that can print more than one type of complex structure. You might also look at the dumpvars module that comes with most, if not all, Perl distributions.

Load this script into the debugger (perl -d 16lst01.pl), use the s command to execute the array assignment, and then display @array with the X array command. Your display should look like this:

@array = (
  0     '1'
  1     '2'
  2     '3'
  3     'ARRAY(0x7c693c)'
  4     '4'
)

You can see that the displayed values are not as informative as you might hope for because of the array reference in element 3. However, because the prtArray() function is designed to print this type of data structure, call it from the debugger using the prtArray(@array); command. This should result in a display like this:

0       '1'
1       '2'
2       '3'
        0       '1'
        1       '2'
        2       '3'
4       '4'

The 1; line of code is used to let you execute the array assignment without the debugger ending. Just ignore it.


next up previous contents
Next: Examples: Using the n Up: Stepping Through Your Script Previous: Stepping Through Your Script
dave@cs.cf.ac.uk