7

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).

kK-Storm
  • 474
  • 1
  • 4
  • 16
  • Did you perhaps plug the output into HTML without converting it to HTML? Doesn't TT have a filter to escape into HTML? Then just wrap it in PRE tags. Keep in mind that neither TT nor Data::Dumper has anything to do with HTML. – ikegami Nov 13 '12 at 10:26
  • What you are speaking about is formatting, but unfortunately that is not what I asked for. I need to get, let's say, filtered content of the variable - without all the column type data and all the other stuff in there - solely the query content (as in my php example) – kK-Storm Nov 13 '12 at 10:31
  • Please show a screenshot of the output you get. I don't fully understand the problem. – simbabque Nov 13 '12 at 11:49
  • As far as I understand the problem it has to do with dumping ORM objects like DBIC objects. – memowe Nov 13 '12 at 12:07
  • Maybe the [Stringification section of DBIC's cookbook](http://p3rl.org/DBIx::Class::Manual::Cookbook#Stringification) can help... – memowe Nov 13 '12 at 12:10

2 Answers2

9

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;
creaktive
  • 5,193
  • 2
  • 18
  • 32
  • Wow, this is pretty much what I was looking for, and it even has Template Toolkit support. I've read the Data::Printer configuration part, and couldn't find an answer to the question - is there a possibility to get also a related resultsets in the object dump? (i.e. 'message' hasMany 'comments', so when I do dump of object 'message' it also shows related comments) – kK-Storm Nov 13 '12 at 13:28
  • You could increase the depth of object traversal: `use Data::Printer max_depth => 5` – creaktive Nov 13 '12 at 14:43
  • @creaktive I tried that, but I only get this: `_relationship_data { state app::Model::DB::State, user app::Model::DB::User },`, not the particular object themselves + somehow it doesn't show all the relations (I have 3 of them in my example with foreign keys state_id, user_id and admin_id - last one is omitted :X) – kK-Storm Nov 13 '12 at 14:57
  • 1
    Maybe you should enable the prefetching of join? – creaktive Nov 13 '12 at 15:05
  • Hm, I added `{ join => 'admin', prefetch => 'admin' }` and I still get no admin object itself, only `_relationship_data { admin adminez::Model::DB::Admin,}` and `related_resultsets { admin DBIx::Class::ResultSet,}`. Maybe I'm interpretting something wrong and there isn't a way to get related objects in object dump? – kK-Storm Nov 13 '12 at 16:01
  • 1
    Sorry @kK-Storm, my bad! This is how you import Data::Printer which traverses ResultSet the way you expect: `use Data::Printer filters => { -external => [qw[DB]]}, class => { linear_isa => 0, expand => 2 };` (prefetch still required!) – creaktive Nov 13 '12 at 17:30
  • Thank you very much, that does it! But in order it to be a perfect match - maybe you could share the code (if it's possible to do so) which prevents `_result_source` and `_inflated_column` hashes to show `internals:`? It's kind of useless for me and takes too much space. p.s. If you'd edit your orig answer with this code added, I'm sure people (and me of course) would really appreciate it :) – kK-Storm Nov 13 '12 at 18:32
  • 1
    @kK-Storm answer edited :) However, I have no idea on how to make Data::Printer stop showing that specific internals... – creaktive Nov 13 '12 at 20:01
0

you just could use

 [% Dumper.dump_html(variable) %]

See: http://template-toolkit.org/docs/modules/Template/Plugin/Dumper.html

Беров
  • 1,383
  • 10
  • 22