Hi

is there a comfortable and DRY way to dump variables?

I mean neither printing "$VAR1" nor having to repeat the name over and over again, and working with both kinds of variables?

For instance always writing:  print Data::Dumper->Dump([\@fontspec],['*fontspec']); is no fun.

I made this hack, which works quite fine so far (ATM for lexicals only):

sub dmp (\[$@%];\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%]\[$@%] +){ # die "to many arguments for dmp()" if @_>10; use PadWalker qw/var_name/; for $struc (@_){ local $,="\n"; my $name=var_name(1, $struc); $name=~s/^[\@\$\%]/\*/; print Data::Dumper->Dump([$struc],[$name]); } }

which called with

dmp @fontspec;

produces the same output:

@fontspec = ( { 'id' => '0', 'family' => 'Helvetica', 'size' => '10' }, { # ... and so on

But I have the uncomfortable feeling of reinventing the wheel...

Cheers Rolf

UPDATE: improved code

UPDATE: hmm maybe Conway's Smart::Comments should be mentioned, but it uses code filters...


In reply to Dumping variables but DRY and simple by LanX

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.