notes blog about

Perl Debugging

There are several ways how to debug a Perl program.


The simplest way is to print the contents of the variables, ex.:

use strict;
use warnings;

# A number between 1 and 100 (both included)
my $secret = int( 1 + rand 100 );

# If DEBUG environment variable set, print out the secret number
print "The secret number is $secret\n"
  if $ENV{DEBUG};


$ DEBUG=1 perl
The secret number is 3

Data::Dumper, YAML

In case you’re dealing with a more complicated data structure (like the $server variable below), you can use Data::Dumper (outputs valid Perl code) or YAML (more compact output):

use strict;
use warnings;
use Data::Dumper;
use YAML;

my $name = 'foo';

my $server = {
    'server' => {
        'imageRef'  => '8a3a9f96-b997-46fd-b7a8-a9e740796ffd',
        'flavorRef' => '2',
        'name'      => $name,
        'metadata'  => { 'My Server Name' => 'Ubuntu 12.10 (Quantal Quetzal)' },

print "== Data::Dumper ==\n";
print Dumper $server;

print "== YAML ==\n";
print Dump $server;

Perl debugger

Perl debugger (perl -d <>) shows each line of code before it executes it. The most common commands:

To have the command line history, you need: