SPUG: debugging (was: Net::Telnet)

Jason Lamport jason at strangelight.com
Fri Sep 7 16:18:02 CDT 2001

At 12:54 PM -0700 9/7/01, David Dyck wrote:
>On Fri, 7 Sep 2001, Jason Lamport wrote:
>>  print( "\$t == $t (". ref( $t ) . ") at line __WHATEVER__\n");
>Perl can insert the correct line number when you use __LINE__
>  print( "\$t == $t (". ref( $t ) . ") at ".__FILE__." line ".__LINE__."\n");
>see perldata.pod
>     The special literals __FILE__, __LINE__, and __PACKAGE__
>     represent the current filename, line number, and package name at that
>     point in your program.  They may be used only as separate tokens; they
>     will not be interpolated into strings.  If there is no current package
>     (due to an empty C<package;> directive), __PACKAGE__ is the undefined
>     value.

Thanks.  I knew there was something like that available, but was 
feeling too lazy to look it up.

Personally, I use a separate function for debugging, which uses 
caller() to do a partial stack trace.  Generally, in any program of 
any complexity, I'll define a subroutine something like this:

	sub debug {
		# pretty-print @_, caller(), caller(1), caller(2), etc.

And then I can insert debugging probes like this:

	debug( $whatever, $variables, $I, $want );

This also makes it very easy to remove the probes later, since I can 
just do a text search for "debug".

I find this approach -- inserting probes directly into the code -- 
far more useful than using an interactive debugger, for several 

*  The debugger tends to force you into doing a linear search for 
bugs, whereas with probes it's easier to do a binary (or N-ary) 
search for the problem.

*  The debugger can only be run from the shell, which can be 
problematic for scripts (such as CGI scripts) that don't expect to be 
run from the shell.  Probes generally work in any environment. (The 
debug routine may need to be modified to print to something other 
than STDOUT -- a log file for example -- but that's usually trivial.)

*  <cranky-old-man-voice> Besides, back when *I* was learning to 
code, we didn't have these fancy new-fangled interactive debuggers 
and compiler type-checking and what-not.  By golly, we had to find 
our *own* bugs!  We also had to code everything in octal.  Uphill. 
Both ways.  In the snow.  And we *liked* it... </cranky-old-man-voice>

Anyway... that's my $0.02 on debugging.  Hope it's helpful to someone.


P.S.  I was joking about coding in octal.  I'm not *that* old!  ;)

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/

More information about the spug-list mailing list