Data::Dumper displaying empty arrays / hashes

N

niall.macpherson

I had a problem with a script of mine and added some 'print Dumper'
calls to try to figure out what was going on. One thing I noticed is
that when I try to dump the contents of an empty array or hash I see no
output at all.

The following test code
##------------------------------------------------------------------------------------------------
use strict;
use warnings;
use Data::Dumper;

my @a_test = ();
print Dumper @a_test; ## Displays nothing
push @a_test , 'aval1';
print Dumper @a_test;

my %h_test = ();
print Dumper %h_test; ## Displays nothing
$h_test{hkey1} = 'hval1';
print Dumper %h_test;

exit(0);
##-----------------------------------------------------------------------------------------------
produces the following output

C:\develop\NiallPerlScripts>datadumper.pl
$VAR1 = 'aval1';
$VAR1 = 'hkey1';
$VAR2 = 'hval1';

Is there a neat way of getting round this ? I would like to continue
using Data::Dumper, but don't want to have to do something like

if(#@fred)
{
print Dumper @fred;
}
else
{
print Dumper '@fred is empty';
}

for every debug statement

Thanks
 
C

Ch Lamprecht

I had a problem with a script of mine and added some 'print Dumper'
calls to try to figure out what was going on. One thing I noticed is
that when I try to dump the contents of an empty array or hash I see no
output at all.

perl -MData::Dumper -e "@a = ();print Dumper \@a;"
$VAR1 = [];

HTH Christoph
 
P

Paul Lalli

I had a problem with a script of mine and added some 'print Dumper'
calls to try to figure out what was going on. One thing I noticed is
that when I try to dump the contents of an empty array or hash I see no
output at all.

The following test code
##------------------------------------------------------------------------------------------------
use strict;
use warnings;
use Data::Dumper;

my @a_test = ();
print Dumper @a_test; ## Displays nothing

Nor should it.

You need to read the Data::Dumper documentation better. Dumper() takes
a list of *references*, and prints the contents of the structures those
references reference. You passed an empty list, hence no references to
examine. If you want to print the contents of a single empty array,
your list of references contains exactly one reference:

print Dumper (\@a_test);

Paul Lalli
 
N

niall.macpherson

Paul Lalli wrote:

You need to read the Data::Dumper documentation better. Dumper() takes
a list of *references*, and prints the contents of the structures those
references reference. You passed an empty list, hence no references to
examine. If you want to print the contents of a single empty array,
your list of references contains exactly one reference:
#

Thanks - I see that now (hangs head in shame ...... :( ) - don't know
how I missed it in the first place

I will go away and re-RTFM immediately :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top