我想像这样漂亮地打印 DBIx::Class::ResultSet 结果:
my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
my $rs = $schema->resultset('Track')->all()
# then print $rs with all of those feilds
我找到了 DBIx::SQLCrosstab::Format 类,但它似乎只适用于自己的查询。
我想像这样漂亮地打印 DBIx::Class::ResultSet 结果:
my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
my $rs = $schema->resultset('Track')->all()
# then print $rs with all of those feilds
我找到了 DBIx::SQLCrosstab::Format 类,但它似乎只适用于自己的查询。
我不知道任何 DBIC漂亮的打印模块,但它很容易从 CPAN 上的无数文本、html 或其他类型的表格输出模块中实现。
下面是我使用的快速工作示例Text::Table
use 5.012;
use warnings;
use List::MoreUtils 'zip';
use Text::Table;
# my database with Album schema from DBIx::Class::Manual::Intro
use MySchema;
my $db = MySchema->connect( "DBI:SQLite:myschema_db" );
my $album = $db->resultset( 'Album' );
# get column names for the Album table
my @cols = $album->result_source->columns;
# create header with these column names
my $table = Text::Table->new( header( @cols ) );
# add each Album row to table output
while (my $cd = $album->next) {
$table->add( map { $cd->get_column( $_ ) } @cols );
}
print $table; # => tabular text output
# adds | separator between header labels
sub header {
my @sep = (\' | ') x @_;
zip @_, @sep;
}
这将与我的测试数据一起输出以下内容:
albumid | artist | title | rank |
1 | Lou Reed | Transformer | |
2 | Lou Reed | Berlin | |
3 | David Bowie | Ziggy Stardust | |
4 | Japan | Tin Drum | |
/I3az/
如果您正在寻找真正漂亮的输出,请使用上面的 draegtun 示例。但是,如果您真的只想能够使用 Data::Dumper 在没有所有源数据的情况下吐出 DBIx::Class::Row 对象,则可以将此挂钩添加到结果类(或者更好的是添加到基本结果所有模式结果的类)
sub _dumper_hook {
$_[0] = bless { %{ $_[0] }, _source_handle=>undef }, ref($_[0]);
}
$Data::Dumper::Freezer = '_dumper_hook';