Search code examples
perlcatalysttemplate-toolkit

Viewing the variable content in a clean way


So the way I'm using to view a variable content is to use Data::Dumper in my template toolkit:

[% USE Dumper %]
[% Dumper.dump(varname) %]

But the result I get is kind of a big mess - all the info about table relations, column types and attrbitues etc.

What I wonder is if there is a way to get a 'clean' variable content - as in only current result from the query being made + related resultsets (i.e. when I used php with cakephp framework there was a 'debug(varname)' command which provided such a result, which looked like this http://pastebin.com/Hut0LnAb).


Solution

  • Data::Printer to the rescue! It's object dump is more human-readable:

    my $obj = SomeClass->new;
    p($obj);
    # produces:
    \ SomeClass  {
        Parents       Moose::Object
        Linear @ISA   SomeClass, Moose::Object
        public methods (3) : bar, foo, meta
        private methods (0)
        internals: {
           _something => 42,
        }
    }
    

    It is compatible with Template Toolkit:

    [% USE DataPrinter %]
    html-formatted, colored dump of the same data structure:
    [% DataPrinter.dump_html( myvar ) %]
    

    And it "knows" how to handle DBIx::Class, too:

    use Data::Printer
        filters => {
            -external => [qw[DB]], # use DB filter
        }, class => {
            expand => 2, # traverse object 2-levels deep
            linear_isa => 0, # hide not-so-relevant information
        };
    
    ...
    
    my $obj = $schema
        ->resultset('AddressState')
        ->search({}, { prefetch => [qw[country]] })
        ->single;
    p $obj;