Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Using The Perl Debugger
Got a Perl problem? Better bring in the debugger.

| Start Me Up |

Most casual Perl users aren't even aware that Perl ships with a debugger, but it does...and it's quite a good one too. In addition to allowing you to arbitrarily jump around your code, it also allows you to execute statements on a line-by-line basis, set breakpoints and watch-expressions, inspect variables and trace program flow (among other things).

In order to demonstrate the features of the debugger, I'll be using some sample Perl scripts. Here's the first one, a script which accepts an identifier as input argument, connect to a database, retrieves a list of email addresses and sends them email.


#!/usr/bin/perl

# load module
use DBI();

unless($ARGV[0])
{
        print "ERROR: Please provide a valid input ID\n\n";
}

# get user input into variable
my $id = $ARGV[0];

# connect
my $dbh = DBI->connect("DBI:mysql:database=db198;host=localhost", "root",
"secret", {'RaiseError' => 1}) or die ("Cannot connect to database");

# set up email message
my $sendmail = "/usr/sbin/sendmail -t";
my $reply_to = "Reply-to: foo\@bar.org";
my $subject  = "Subject: FOOBAR";
my $content = "Hello and how are you doing? This is the message body";

# now retrieve email addresses and send mail to each
my $sth = $dbh->prepare("SELECT email FROM users WHERE users.id = '$id'");
$sth->execute();
while(my $ref = $sth->fetchrow_hashref())
{
        my $to = $ref->{'email'};
        open(SENDMAIL, "|$sendmail") or die "Cannot send mail: $!";
        print SENDMAIL $reply_to ."\n";
        print SENDMAIL $subject . "\n";
        print SENDMAIL "To: " . $to . "\n";
        print SENDMAIL "Content-type: text/plain\n\n";
        print SENDMAIL $content;
        close(SENDMAIL);
}

# clean up
$dbh->disconnect();


Perl's built-in debugger can be invoked simply by adding the -d option to your Perl command line, as below:


$ perl -d mailer.pl 877
Loading DB routines from perl5db.pl version 1.19
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(mailer.pl:6): unless($ARGV[0])
main::(mailer.pl:7): {
  DB<1>


This will pop you into the debugger and place you at the first line of your script.

The


DB<1>


marker is the debugger command prompt; this is where you will enter debugger commands. The number in angle braces following the prompt keeps track of the number of commands you've entered, so that you can easily reuse a previous command.

Commands entered at the prompt which are not debugger commands are evaluated as Perl statements, and executed - as illustrated below:


  DB<2> print 8+10;

  DB<3> 18

  DB<3> $a=10/2

  DB<4> print $a;

  DB<5> 5


You can enter multi-line commands into the debugger by separating them with a backslash, as in the example below:


  DB<2> @flavours = ("vanilla", "chocolate", "strawberry");

  DB<3> for $f (@flavours) {   \
  cont:     print "$f\n";      \
  cont: };
vanilla
chocolate
strawberry


Help is available via the "h" command (don't be intimidated, most of the following will be explained over this article),


  DB<1> h
List/search source lines:               Control script execution:
  l [ln|sub]  List source code            T           Stack trace
  - or .      List previous/current line  s [expr]    Single step [in expr]
  v [line]    View around line            n [expr]    Next, steps over subs
  f filename  View source in file         <CR/Enter>  Repeat last n or s
  /pattern/ ?patt?   Search forw/backw    r           Return from subroutine
  M           Show module versions        c [ln|sub]  Continue until position
Debugger controls:                        L           List break/watch/actions
  o [...]     Set debugger options        t [expr]    Toggle trace [trace
expr]
  <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set
breakpoint
  ! [N|pat]   Redo a previous command     B ln|*      Delete a/all breakpoints
  H [-num]    Display last num commands   a [ln] cmd  Do cmd before line
  = [a val]   Define/list an alias        A ln|*      Delete a/all actions
  h [db_cmd]  Get help on command         w expr      Add a watch expression
  h h         Complete help page          W expr|*    Delete a/all watch exprs
  |[|]db_cmd  Send output to pager        ![!] syscmd Run cmd in a subprocess
  q or ^D     Quit                        R           Attempt a restart
Data Examination:     expr     Execute perl code, also see: s,n,t expr
  x|m expr       Evals expr in list context, dumps the result or lists
methods.
  p expr         Print expression (uses script's current package).
  S [[!]pat]     List subroutine names [not] matching pattern
  V [Pk [Vars]]  List Variables in Package.  Vars can be ~pattern or !pattern.
  X [Vars]       Same as "V current_package [Vars]".
  y [n [Vars]]   List lexicals in higher scope <n>.  Vars same as V.
For more help, type h cmd_letter, or run man perldebug for all docs.


and you can obtain detailed help with the "h h" command


  DB<1> h h

Help is currently only available for the new 580 CommandSet, if you
really want old behaviour, presumably you know what you're doing.

T               Stack trace.
s [expr]        Single step [in expr].
n [expr]        Next, steps over subroutine calls [in expr].
<CR>            Repeat last n or s command.
r               Return from current subroutine.
c [line|sub]    Continue; optionally inserts a one-time-only breakpoint
                at the specified position.
l min+incr      List incr+1 lines starting at min.
l min-max       List lines min through max.
l line          List single line.
...


Once your script has completed exiting, you can restart it with the "R" command, as below,


Debugged program terminated.  Use q to quit or R to restart,
  use O inhibit_exit to avoid stopping after program termination,
  h q, h R or h O to get additional info.
  DB<10> R
Warning: some settings and command-line options may be lost!

Loading DB routines from perl5db.pl version 1.19
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(mailer.pl:6): unless($ARGV[0])
main::(mailer.pl:7): {
  DB<10>


or exit the debugger (this works even during script execution) by typing "q", and you will be immediately returned to your command prompt.


  DB<1> q


Don't do that just yet, though - after all, you're here to learn more about this strange and wonderful new animal, and it would be rude to leave so quickly. Next up, I'll show you how to step through a script and execute it on a line-by-line basis.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback