Viewing the variable content in a clean way
Asked Answered
O

2

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

Organelle answered 13/11, 2012 at 10:21 Comment(5)
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.Shiksa
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)Organelle
Please show a screenshot of the output you get. I don't fully understand the problem.Displant
As far as I understand the problem it has to do with dumping ORM objects like DBIC objects.Siloxane
Maybe the Stringification section of DBIC's cookbook can help...Siloxane
D
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;
Diwan answered 13/11, 2012 at 10:34 Comment(8)
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)Organelle
You could increase the depth of object traversal: use Data::Printer max_depth => 5Diwan
@Diwan 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)Organelle
Maybe you should enable the prefetching of join?Diwan
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?Organelle
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!)Diwan
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 :)Organelle
@Organelle answer edited :) However, I have no idea on how to make Data::Printer stop showing that specific internals...Diwan
J
0

you just could use

 [% Dumper.dump_html(variable) %]

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

Joinville answered 21/11, 2012 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.