Some pretty printing options for variables

Recently I needed something with the same functionality of Data::Dumper but prettier printing for dealing with nested structures. First stop Metacpan. I type in 'Data::Dumper' and the auto complete dropdown lists some suggestions and I try a few. Ovid's Data::Dumper::Names gives me the names of variables I pass in by reference. With Data::Dumper you would see this

$VAR2 = [ 
          [ 
            'plain', 
            'white' 
          ]
        ];

Now with Data::Dumper::Names it looks like this:


@color_codes = (
[
'plain',
'white'
]
);

While this is better if I set one of the values to '\e[0;38;5;023m' the output is now incorrect.

@color_codes = (
[
'plain',
''
]
);

Data::Dumper and Data::Dumper::Names both interpret the above value and print out in color instead of printing the value. While reading the OSCON 2011 notes for Damian Conway over at PerlBuzz I saw an example of Data::Show and decided to try it out. Here is the sample code followed by the output.

use Data::Show;
my @color_codes = ( 'plain', '\e[0;38;5;023m' );
show ( @color_codes );

$ ./junk.pl
======( @color_codes )====================[ 'junk.pl', line 12 ]======

["plain", "\\e[0;38;5;023m"]

Now I get values I expected, the variable name and the line number all from the default show() behavior. The point? Perl sites are good and CPAN holds many treasures.

Fun useless fact, if you run the following code with Data::Dumper or Data::Dumper::Names the numerical escape value prints correctly with re.pl.

use Data::Dumper;
my @color_codes = ( 'plain', '\e[0;38;5;023m');
print Dumper (@color_codes);

                            
                        

2 Comments

Excellent! I'm glad someone got something out of that huge pile of notes.

Leave a comment

About Kimmel

user-pic I like writing Perl code and since most of it is open source I might as well talk about it too. @KirkKimmel on twitter