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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top