I take a different approach to this issue of making "printf debugging" easier: I have a binding in my text editor so that when I press Ctrl-K, it will turn the variables on the current line into a printout statement, like so:
# In Perl, I type: %some_hash, $some_scalar, @some_list, some_func() # and then hit Ctrl-K to turn it into: print STDERR "DBG 950($$): ([\%some_hash, \$some_scalar, \@some_list +, some_func()]):\n\t", Dumper([\%some_hash, $some_scalar, \@some_list +, some_func()]);
The random number after DBG makes it easy to search for that debug line. I have similar bindings for shell, java, C, and other languages, some of which need some hints with the types (e.g., my_name:s becomes my_name(%s)). Here's the binding in my Vim perl.vim syntax file:
nmap <C-K> <ESC>!!print_perl_debug STDERR %<RETURN>:w<CR>
And here's print_perl_debug:
#!/usr/bin/perl -w $handle = shift || 'STDERR'; $file = shift || '/dev/null'; $is_php = system(q(grep -q '^<\?PHP$' ) . $file) == 0; if ($is_php) { while (<STDIN>) { chomp; s/(^\s*)//; $space = $1; $rand = int(rand 900) + 100; print qq(${space}print 'DBG $rand: $_: ' . "[$_]\\n";\n); } } else { $do_use = system "grep -q '^[^#]*use Data::Dumper' $file"; while (<STDIN>) { chomp; s/(^\s*)//; $space = $1; $use = $do_use ? " use Data::Dumper;" : ''; s/([@%])/\\$1/g; s/\$\\\@/\$\@/g; # fix up $@ $_ = "[$_]" if /,/; $rand = int(rand 900) + 100; $printable = $_; $printable =~ s/(\$|")/\\$1/g; my $last = /\S/ ? qq(($printable):\\n\\t", Dumper($_);$use\n) +: qq(\\n";\n); print $space, 'print ', ($handle eq 'STDOUT' ? '' : "$handle " +), qq{"DBG $rand(\$\$): $last}; } }

In reply to Re: How to extract the name of a variable? by PreferredUserName
in thread How to extract the name of a variable? by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.