Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Because I often deal with data containing every char on the keyboard, I use:

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)}


In reply to Re^3: Unbelievably Obvious Debugging Tip by radiantmatrix
in thread Unbelievably Obvious Debugging Tip by dreadpiratepeter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-23 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found