in reply to How to extract the name of a variable?
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:# 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()]);
And here's print_perl_debug:nmap <C-K> <ESC>!!print_perl_debug STDERR %<RETURN>:w<CR>
#!/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}; } }
|
|---|