Re: Unbelievably Obvious Debugging Tip
by Abigail-II (Bishop) on Apr 27, 2004 at 14:29 UTC
|
| [reply] [Watch: Dir/Any] |
|
I use cat -A for ages. Yes, it is a good thing.
Also, I often use cat -v to make nonprintable characters printable,
so that if there's a C-N it wouldn't mess up my terminal,
but if it's only text, it would not change.
It's also a great way to see C-M's.
| [reply] [Watch: Dir/Any] |
|
My only concern with that is people who work from a non-UNIX terminal. As I recall, the closest windows alternative (without loading Cygwin) is type, which of course has none of those options. Personally, I tend to agree with the original poster, I delimit my variables when writing logging or debugging output.
| [reply] [Watch: Dir/Any] |
|
Man, those Windows users are such lazy bastards. Too damn lazy to install a decent Unix toolkit, and too damn lazy to write a trivial Perl program. All they can do is whine that a solution is too Unix specific (never mind the fact that an open source solution for any Unix toolkit is available, not something we can say about Windows tools, can we?) Djees, you give them Perl - the portable Unix - and they still aren't satisfied.
#!/usr/bin/perl
use strict;
use warnings;
no warnings qw /syntax/;
use Getopt::Long;
my ($show_ends, $show_tabs, $show_nonprinting);
GetOptions ("A|show-all" => sub {$show_ends = 1;
$show_tabs = 1;
$show_nonprinting = 1},
"e" => sub {$show_ends = 1;
$show_nonprinting = 1},
"E|show-ends" => sub {$show_ends = 1},
"t" => sub {$show_tabs = 1;
$show_nonprinting = 1},
"T|show-tabs" => sub {$show_tabs = 1},
"v|show-nonprinting" => sub {$show_nonprinting = 1},
);
while (<>) {
chomp;
s/([\x80-\xFF])/"M-" . chr (ord ($1) - 0x80)/eg if $show_nonprinti
+ng;
s/([\x00-\x08])/"^" . chr (ord ($1) + 0x40)/eg if $show_nonprinti
+ng;
s/([\x0A-\x1F])/"^" . chr (ord ($1) + 0x40)/eg if $show_nonprinti
+ng;
s/\x7F/^?/g if $show_nonprinti
+ng;
s/\x09/^I/g if $show_tabs;
s/$/\$/ if $show_ends;
print "$_\n";
}
__END__
Took less than 15 minutes to write and test.
Abigail | [reply] [Watch: Dir/Any] [d/l] |
|
Those who work without a decent set of tools get no sympathy from me. Either they are fools, or they work for fools. In both cases, the fool is the one who suffers from reduced productivity, which is as it should be.
| [reply] [Watch: Dir/Any] |
|
|
|
Wow! You learn something new every day. Thanks for the tip!
| [reply] [Watch: Dir/Any] |
Re: Unbelievably Obvious Debugging Tip
by eserte (Deacon) on Apr 27, 2004 at 14:33 UTC
|
I even always use Data::Dumper for debugging output. So I can also spot (invisible) control characters, and I can also dump complex data structures for free. To safe key strokes I use an emacs macro for this:
(defun perl-insert-data-dumper ()
(interactive)
(insert "require Data::Dumper; print STDERR \"Line \" . __LINE__ . \
+", File: \" . __FILE__ . \"\\n\" . Data::Dumper->new([],[])->Indent(1
+)->Useqq(1)->Dump; # XXX\n")
(forward-char -40)
)
(define-key global-map [C-f12] 'perl-insert-data-dumper)
| [reply] [Watch: Dir/Any] [d/l] |
|
Allow me to recommend Data::Dump::Streamer instead.
Yes this blatant advertising. But if you talking about for debugging purposes you might as well use something that gets it right 99.9999% of the time instead of 98% of the time. Oh, and I suspect youll find the output is a lot easier to read. :-)
---
demerphq
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
| [reply] [Watch: Dir/Any] [d/l] |
|
i often use Data::Dumper, too. my macro (vim) is:
imap ddumper <ESC>gglouse Data::Dumper; $Data::Dumper::Indent = 1; $Da
+ta::Dumper::Sortkeys = 1;<ESC>``
although this is just the use-stuff; maybe i should add
a macro for the actual printout. | [reply] [Watch: Dir/Any] [d/l] |
Re: Unbelievably Obvious Debugging Tip
by pelagic (Priest) on Apr 27, 2004 at 14:16 UTC
|
100% agreed!
It looks obvious but it's often disregarded anyway.
I even do:
print "str:$str:\n";
| [reply] [Watch: Dir/Any] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Unbelievably Obvious Debugging Tip
by TomDLux (Vicar) on Apr 27, 2004 at 15:04 UTC
|
Definitely! I got in the habit of single-quoting my variables after getting taking a few hours to notice the extra line of output. Recently I'm getting use Data::Dumper more often, which automates the provision of quotes.
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] [Watch: Dir/Any] |
Re: Unbelievably Obvious Debugging Tip
by Theo (Priest) on Apr 27, 2004 at 17:34 UTC
|
I ran into the same sort of problem when printing several variables on the same line. It wasn't immediately clear where one variable ended and the next one began. I started printing them with a single '^' and with no spaces between each value. That caught several problems for me. ++ dreadpiratepeter.
-Theo-
(so many nodes and so little time ... )
| [reply] [Watch: Dir/Any] |
Re: Unbelievably Obvious Debugging Tip
by matija (Priest) on Apr 27, 2004 at 16:10 UTC
|
print "(".join(")(",@arr).")\n";
| [reply] [Watch: Dir/Any] [d/l] |
|
local $" = ", ";
print "[@arr]\n";
Abigail | [reply] [Watch: Dir/Any] [d/l] |
|
When I don't use Data::Dumper, I do something like:
print join "|", "", $list, $of, $vars, ""
with the beginning and ending empty string making join put the separator there.
| [reply] [Watch: Dir/Any] [d/l] |
|
print STDERR join("\x1E", @list),"\x1E\n";
This gives me an output with visible record separators (ASCII 0x1E is actuall "Record Separator") quite cleanly, and I use STDERR to avoid bufferring. Default buffering of STDOUT has bitten my debug routines a few times.
If I'm working a large project, I use my own little module that exports a number of debugging "tools", including this:
sub dbg {
return unless $DEBUG;
my $dump = 0;
($dump = 1 && shift) if ($_[0] eq ':DUMP:');
$LOGFILE = open_logfile();
foreach my $item (@_) {
$str = (($dump && ref $item) ? Dumper($item) : $item)."\x1E\n
+";
print $LOGFILE POSIX::strftime("%Y%m%d:%H.%M.%S\x1E",localtime
+).$str if defined $LOGFILE;
print STDERR $str unless $QUIET;
}
}
The open_logfile sub returns an opened file handle, or opens a new one if need be, to "DEBUG.LOG". Debug items are written to STDERR unless I set $QUIET, and they are always written to the logfile, prepended by a timestamp.
And, of course, if I unset $DEBUG, then it's basically a no-op.
Run the following with 'you stupid foo' as arguments:
#!/usr/bin/perl
use Private::Debug 'dbg';
$Private::Debug::DEBUG = 1;
$Private::Debug::QUIET = 0;
dbg("Hello there", "You fool");
dbg(':DUMP:', "ARGV:", \@ARGV);
Results in
#DEBUG.LOG
20041214:17.58.22▲Hello there▲
20041214:17.58.22▲You fool▲
20041214:17.58.22▲ARGV:▲
20041214:17.58.22▲$VAR1 = [
'you',
'stupid',
'foo'
];
▲
#STDERR
Hello there▲
You fool▲
ARGV:▲
$VAR1 = [
'you',
'stupid',
'foo'
];
▲
It works quite well. The '?' you might see actually shows up as an upward triangle in a terminal, so it is very clear.
radiantmatrix
require General::Disclaimer;
s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Unbelievably Obvious Debugging Tip
by etcshadow (Priest) on Apr 28, 2004 at 02:13 UTC
|
In my shell, I just drag my mouse over potential whitespace... if it highlights, then it is whitespace, otherwise not.
Also, if I'm examining output, I'll pipe it into vi - (rather than more or less), where, again, trailing whitespace is distinguishible in various ways.
It's personal style, I guess.
One last thing... if you've got two strings showing as different even though they look, by visual examination, the same... the very first thing you should be doing is a character-by-character diff (many different ways of accomplishing this) and/or looking at them in a hex dump. That's just common sense.
------------
:Wq
Not an editor command: Wq
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Unbelievably Obvious Debugging Tip
by spacewarp (Pilgrim) on Apr 27, 2004 at 19:28 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Unbelievably Obvious Debugging Tip
by Aragorn (Curate) on Apr 29, 2004 at 08:28 UTC
|
I found this out the hard way, years ago. print "<$something>\n"; has saved me much aggravation and time :-)
Arjen | [reply] [Watch: Dir/Any] [d/l] |