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:

Data::Dumper::Declare

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?

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^2: How to get variable name in trace message by tobyink
in thread How to get variable name in trace message by ChrisBeall

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.