LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

It's somehow a p!ta (or more diplomatic "not a pleasure") to dump variables together with their names.

Data::Dumper has a very cumbersome way to do it, where the variables have to be repeated, once as reference and then as string holding the name.

Data::Dump has no mechanism at all for names and expects me to handle the printing of the name.

Before I reinvent the wheel¹, is their already a good way to simply say

dmp \@a;

and to automatically get something like

\@a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
edit

When offering answers please keep in mind that different symbols can hold the same references or globs.

Reverse engineering the name is not trivial.

Cheers Rolf

( addicted to the Perl Programming Language)

¹) well I already invented using B::Deparse, but IIRC when I asked some years ago and there was no clean way ...

updated

it helps to think about how such a module should be named! :)

see

  • Data::Dumper::Simple (uses source filter)
  • Data::Dumper::Names (depends on PadWalker and has limitations)

    and maybe also Smart::Comments should be mentioned (another sourcefilter)

    anything else?

  • Replies are listed 'Best First'.
    Re: dumping variables automatically with their name?
    by choroba (Cardinal) on Mar 23, 2014 at 22:31 UTC
        yep thx! :)

        ... but still an open issue...

        update
        actually many thanks, I searched my hard disk for this posted code, I forgot that I "stored" it in the cloud... ;-)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

    Re: dumping variables automatically with their name?
    by tobyink (Canon) on Mar 24, 2014 at 10:49 UTC

      As well as Data::Dumper::Simple and Data::Dumper::Names I wrote Data::Dumper::Declare (but it's BackPAN-only now). I think my version provides slightly nicer output than the other two, but I removed it from CPAN because I think there are enough modules to do this already.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
    Re: dumping variables automatically with their name?
    by Rhandom (Curate) on Mar 24, 2014 at 14:52 UTC
      There is another method to get the name other than pad walking or name passing. I used this method in the 11 year old CGI::Ex::Dump module (looks like the POD for that module is rather lackluster - guess I should update it after 11 years). We've used a variant of this for some time.

      Whenever we debug we also like to see the line location - once we have parsed for the line location it is trivial to open the code and look for that line and do a very barebones parse. Apologist notes: This may not be academically correct, but we don't care - we're debugging. The output will also not be right if the source file changes, or the debug line is too complex - but again I don't care about a perfect solution - I want a fast good enough solution. Maybe someday we'll update it to a padwalker, but it is really rather trivial as it is. Here is a sample:

      $ cat sample.pl use strict; use CGI::Ex::Dump qw(debug); my $a = 123; my @b = qw(1..10); my $obj = bless {}, __PACKAGE__; debug; debug $a, \@b, $obj; debug "Arbitrary String "x2; debug {"a".."f"}; $ perl sample.pl debug: sample.pl line 6 debug: sample.pl line 7 $a = 123; \@b = [ "1..10" ]; $obj = bless( {}, 'main' ); debug: sample.pl line 8 String = "Arbitrary String Arbitrary String "; debug: sample.pl line 9 {"a".."f"} = { a => "b", c => "d", e => "f" };
      The actual dumping is done by Data::Dumper, so setting all Data::Dumper options will control the output. It should be easy enough to replace the output with Data::Dump, or Data::Dumper::Simple, or even JSON or YAML, or any of the other 100 ways to dump out data. We used Data::Dumper because 11 years ago it was what there was.

      Internally we use a variation of this codebase called simply "Debug". By default debug goes to STDOUT, but we have variants called debug_warn that goes to STDERR and a debug_dlog variant that will go directly to our perl based system logger.

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
        Thanks! :)

        ... took a look into the source...

        May I ask why you are reopening (i.e. reading again) the files if DATA is already pointing to them?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

    Re: dumping variables automatically with their name?
    by trwww (Priest) on Mar 25, 2014 at 22:19 UTC
      I use Data::Dumper->Dump( ARRAYREF, ARRAYREF ). You have to pass in the name, but it gives you what you're asking for:
      use warnings; use strict; use Data::Dumper; my @data = (qw(foo bar bazz)); print Data::Dumper->Dump([\@data], ["data"]);
      outputs:
      $data = [ 'foo', 'bar', 'bazz' ];
        I knew, thats what I tried to avoid.

        Cheers Rolf

        ( addicted to the Perl Programming Language)