Data-Printer icon indicating copy to clipboard operation
Data-Printer copied to clipboard

show variable name as __VAR__

Open hakonhagland opened this issue 10 years ago • 6 comments

Hi guys!

Background: It could be useful to have the original variable name included in the output from the p command.

When I debug a program I often have many p statements following each other. In order to easily see which output belongs to which variable, it would be nice if I could type:

my $var = 3;
p $var;

with output: $var = 2. I would like to avoid repeating myself like:

say "printing \$var"; p $var;

To me, this seems to be a difficult problem, that would involve parsing Perl code and probably should be done using the PPI module for parsing. However, my first strategy for building a solution to this was to get started somehow :-) something is better than nothing. So this pull request is indeed a very crude first attempt, and I hope it will be accepted so it could be gradually improved with time :-)

hakonhagland avatar Nov 10 '15 23:11 hakonhagland

Added PPI support and some tests..

hakonhagland avatar Nov 22 '15 20:11 hakonhagland

Nice one! I've been wanting this same thing for a while now.

jayallen avatar Nov 24 '15 15:11 jayallen

I am not sure why the tests failed here.. the tests passes on my machine using perl v5.20 on Ubuntu 14.10.

hakonhagland avatar Dec 01 '15 22:12 hakonhagland

@hakonhagland @jayallen YES! That's a very nice feature indeed. Sorry for not reviewing this before, I'll definitely look into reviewing and merging this right after the holidays.

Thanks!

garu avatar Dec 29 '15 11:12 garu

How about using PadWalker? Data::Dumper::Names and Data::Dump::Streamer both do this, though they are both really old.

grr avatar Dec 25 '22 13:12 grr

How about using PadWalker?

@grr Yes I have considered those also, see https://github.com/hakonhagland/Data-Printer-Plugin-Caller-PPI/blob/b8d9eb2ec5ad2c5a9d6addc5a2f7cf91365b1f4d/lib/Data/Printer/Plugin/Caller/PPI/Parser.pm#L21

hakonhagland avatar Dec 25 '22 16:12 hakonhagland