Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Unbelievably Obvious Debugging Tip

by matija (Priest)
on Apr 27, 2004 at 16:10 UTC ( #348557=note: print w/replies, xml ) Need Help??

in reply to Unbelievably Obvious Debugging Tip


Here's what I use when I have to dump an array:

print "(".join(")(",@arr).")\n";

Replies are listed 'Best First'.
Re: Unbelievably Obvious Debugging Tip
by Abigail-II (Bishop) on Apr 27, 2004 at 16:28 UTC
    I prefer:
    local $" = ", "; print "[@arr]\n";


Re: Re: Unbelievably Obvious Debugging Tip
by Anomynous Monk (Scribe) on Apr 27, 2004 at 16:52 UTC
    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.

      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

      20041214:17.58.22▲Hello there▲
      20041214:17.58.22▲You fool▲
      20041214:17.58.22▲$VAR1 = [
      Hello there▲
      You fool▲
      $VAR1 = [

      It works quite well. The '?' you might see actually shows up as an upward triangle in a terminal, so it is very clear.

      require General::Disclaimer;
      s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://348557]
and the monks are chillaxin'...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2018-08-18 21:43 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (186 votes). Check out past polls.