in reply to Re: How to get variable name in trace message
in thread How to get variable name in trace message
While I still stand by the "more effort than it's worth" comment, I've been meaning to get more stuck into Devel::Declare lately, so I had a go, and this is what I came up with:
It gives you a Dumper function just like Data::Dumper (in fact, it uses Data::Dumper internally, so respects things like $Data::Dumper::Sortkeys), but figures out the variable names being dumped. So you can do this:
use Data::Dumper::Declare; my $foo = 1; my $bar = 'Hello World'; my @baz = qw(1 2 3); print Dumper($foo, substr($bar, 0, 5), @baz);
... and you get:
$foo = 1; $EXPR = 'Hello'; \@baz = [ 1, 2, 3 ];
(Note that unlike Data::Dumper, you can't just eval the output. An assignment like \@bar = [...] is illegal.)
Data::Dumper::Declare isn't on CPAN yet. Do people think it useful enough to upload?
Update: just pushed a change which allows arrays and hashes to be dumped more nicely. e.g.
@baz = ( 1, 2, 3 );
Update 2: Data::Dumper::Declare is now on CPAN.
Update 3: Given this:
use Data::Dumper::Declare; my @foo = qw(Hello World); print Dumper( @foo, $foo[0], $foo[1], join('::', @foo), );
The output is:
@foo = ( 'Hello', 'World' ); $foo[0] = 'Hello'; $foo[1] = 'World'; $EXPR = 'Hello::World';
Can anyone suggest something better than $EXPR in the output? It's certainly possible to output join('::', @foo) = 'Hello::World'; but I'm not especially fond of that. What do other people think?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to get variable name in trace message
by rovf (Priest) on Jun 15, 2012 at 11:00 UTC | |
|
Re^3: How to get variable name in trace message
by SuicideJunkie (Vicar) on Jun 15, 2012 at 15:36 UTC | |
|
Re^3: How to get variable name in trace message
by ChrisBeall (Novice) on Jun 18, 2012 at 02:34 UTC |