develooper Front page | perl.beginners | Postings from April 2002

Re: Debugging and 'uninitialized value in concatenation' question

Thread Previous
From:
Chas Owens
Date:
April 26, 2002 07:52
Subject:
Re: Debugging and 'uninitialized value in concatenation' question
Message ID:
1019832379.21829.191.camel@tert.icallinc.com
On Thu, 2002-04-25 at 14:49, Jason Frisvold wrote:
> Judging from the subject lines coming across the list today, I think I'm
> gonna toss this question out there and then duck and cover...  :-)
> 
> I have 2 actual questions.  I'll try to be as specific as possible..
> :-)
> 
> Question #1.
> 
> When I'm writing code, I want to be able to view as much output as
> possible so I can trace through everything and ensure that it is,
> indeed, running correctly.  I've gone as far as writing my own debug
> module that uses "levels" and outputs everything in color.
> (Term::ANSIColor is a lot of fun to play with)...  One major side effect
> of this is that my code will begin to run slower due to all of the
> debugging...
> 
> So, the obvious answer is to turn off the debugging.  However, even with
> my magic "debug off switch" (see below for a better explanation), the
> calls to the debug routines are still being made.  Is there any sort of
> #IFDEF that can be done in perl, or do I actually have to comment out
> (or remove) each call?
> 
> My magic "debug off switch" is nothing more than a simple if/then
> statement.  It works on the principle that if $IDebugLevel is a positive
> integer greater than 0, then I want to debug.
> 

You can use cpp (the c preprocessor) with a filter like this:

#!/usr/bin/perl -w

use strict;
use Filter::cpp; #from the Filter module on CPAN

#DEFINE DEBUG 1

print "this is normal code\n";

#IF DEBUG
print "this is debug code that simply goes away
when DEBUG is set to 0.\n"
#END IF

If you  are on a system that doesn't have cpp (almost all Unix boxen
have cpp in one form or another) you can still roll your own filter
similar to this:

<example>
package DEBUG;

use Filter::Util::Call;
use Carp;

sub unimport {
	my ($type, @other_args) = @_;
	my $in_debug      = 0;

	filter_add (sub {
		$l++;

		my $status = filter_read;
		croak "unbalenced '#DEBUG'" if $status == 0 and $in_debug;

		if (/^\s*#DEBUG ON/) {
			carp "nested #DEBUG ON is dangerous" if ++$in_debug > 1;
		} elsif (/^\s*#DEBUG OFF/) {
			croak "unbalenced '#DEBUG'" if --$in_debug < 0;
		} else {
			s/^/#/ if ($in_debug);
		}

		return $status;
	});
}

sub import {
	filter_del();
}

1;
</example>

This module will allow you to say things like:

<example>
#!/usr/bin/perl

use lib '.';

no DEBUG; #turn off debugging code

my $test = 0;

#DEBUG ON
print "This should not be printed if no DEBUG is used\n";
#DEBUG OFF

print "this should always print\n";

use DEBUG; #turn debug code back on

#this code should execute
#DEBUG ON
print "this should only show up if no DEBUG is not in effect\n";
$test = 10;
#DEBUG OFF

no DEBUG; #turn debug back off

#DEBUG ON
print "This shouldn't be printed because of the no DEBUG above\n";
#DEBUG OFF

print "this \$test = ($test)\n"
</example>

> Question #2.
> 
> Relating to the debugging, there are several instances where I have
> variables that are only defined based on the definition of other
> variables that exist elsewhere.  Kind of like :
> 
> sub dummy {
> if ($a == 10) { my $b = 0; }
> }
> 
> $b is a local variable, so whenever the subroutine is exited, $b
> vanishes into the ether.  The problem is that I have $b in several debug
> statements because I want to see it when it's used, but I don't want to
> have to create a huge if/then structure to determine what variables are
> being used.
> 
> So, the general question becomes, how should variables like this be
> handled?  Should I be initializing them every iteration even if they go
> unused, or should I be using some sort of defined() statement?  What is
> the "proper" way to handle this in perl?  If this was C, I would have to
> say that the variables are declared each time...  I'm just curious if
> there's a better way to handle this in perl...
> 
> 
> TIA,
> 
> 
> ---------------------------
> Jason H. Frisvold
> Senior ATM Engineer
> Engineering Dept.
> Penteledata
> CCNA Certified - CSCO10151622
> friz@corp.ptd.net
> ---------------------------
> "I love deadlines.  I especially like the whooshing sound they make as
> they go flying by." -- Douglas Adams [1952-2001]
> 
> 
-- 
Today is Sweetmorn the 43rd day of Discord in the YOLD 3168
You are what you see.

Missile Address: 33:48:3.521N  84:23:34.786W


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About