5.1. Dumping Your Data

That is all well and good for scalars and arrays, but it won't even handle hashes, let alone complex lists of lists. A good way to deal with those is to use the Dumper function in the Data::Dumper module which will let you print out, nicely formatted, anything you give it, no matter how complex:

use Data::Dumper;
# set %state from some database or flat file
warn Dumper(\%state);

which produces

$VAR1 = {
          'WI' => {
                    'BIRD'         => 'Robin',
                    'NAME'         => 'Wisconsin',
                    'LARGEST_CITY' => 'Milwaukee',
                    'CAPITAL'      => 'Madison',
                    'FLOWER'       => 'Wood Violet'
                  },
          'MS' => {
                    'BIRD'         => 'Mockingbird',
                    'NAME'         => 'Mississippi',
                    'LARGEST_CITY' => 'Jackson',
                    'CAPITAL'      => 'Jackson',
                    'FLOWER'       => 'Magnolia'
                  },
[...]

Note that we passed a reference to the hash we were examining to Dumper. If we were to pass the hash itself, all the elements of the hash %state would become a list input to the routine, and each would be displayed as though it were a separate variable (hence given the labels $VAR1, $VAR2, etc.). By passing a reference to %state, Dumper sees just the one variable, a reference to a hash, and can format its output as keys and associated values. Dumper can also detect references it has previously followed and thus minimize the size of the output necessary (and avoid problems with circular references).

Use the Data::Dumper module to print out a formatted dump of any variable or hierarchical data structure.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset