Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 3307dfca

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 3307dfcae96e3c273f886998edb24ca2e80b7287
  • Vorgänger bd08a875
  • Nachfolger 665fb6bc

Console: Hilfsfunktionen ptab() und pobj()

Unterschiede anzeigen:

scripts/console
48 48
use SL::Locale;
49 49
use SL::LXDebug;
50 50
use Data::Dumper;
51
use List::Util qw(max);
51 52

  
52 53
# this is a cleaned up version of am.pl
53 54
# it lacks redirection, some html setup and most of the authentication process.
......
129 130
  Data::Dumper::Dumper(@_);
130 131
}
131 132

  
133
sub ptab {
134
  my @rows = ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_;
135
  return '<empty result set>' unless @rows;
136

  
137
  my @columns = sort keys %{ $rows[0] };
138
  my @widths  = map { max @{ $_ } } map { my $column = $_; [ length($column), map { length("" . ($_->{$column} // '')) } @rows ] } @columns;
139
  my @output  = (join ' | ', map { my $width = $widths[$_]; sprintf "\%-${width}s", $columns[$_] } (0..@columns - 1));
140
  push @output, join('-+-', map { '-' x $_ } @widths);
141
  push @output, map { my $row = $_; join(' | ', map { my $width = $widths[$_]; sprintf "\%-${width}s", $row->{ $columns[$_] } // '' } (0..@columns - 1) ) } @rows;
142

  
143
  return join("\n", @output);
144
}
145

  
146
sub pobj {
147
  my ($obj) = @_;
148
  return '<no object>' unless $obj;
149

  
150
  my $ref        =  ref $obj;
151
  $ref           =~ s/^SL::DB:://;
152
  my %primaries  =  map { ($_ => 1) } $obj->meta->primary_key;
153
  my @columns    =  map { "${_}:" . ($obj->$_ // 'UNDEF') } sort $obj->meta->primary_key;
154
  push @columns,    map { "${_}:" . ($obj->$_ // 'UNDEF') } grep { !$primaries{$_} } sort map { $_->{name} } $obj->meta->columns;
155

  
156
  return "<${ref} " . join(' ', @columns) . '>';
157
}
158

  
132 159
1;
133 160

  
134 161
__END__
......
165 192
different depth, you'll have to change that. A nice feature would be to
166 193
configure that, or at least to be able to change it at runtime.
167 194

  
195
=head2 ptab C<@data>
196

  
197
Returns a tabular representation of C<@data>. C<@data> must be an
198
array or array reference containing hash references. Column widths are
199
calculated automatically.
200

  
201
Undefined values are represented by an empty column.
202

  
203
Example usage:
204

  
205
    ptab($dbh->selectall_arrayref("SELECT * FROM employee", { Slice => {} }));
206

  
207
=head2 pobj C<$obj>
208

  
209
Returns a textual representation of the L<Rose::DB> instance
210
C<$obj>. This includes the class name, then the primary key columns as
211
name/value pairs and then all other columns as name/value pairs.
212

  
213
Undefined values are represented by C<UNDEF>.
214

  
215
Example usage:
216

  
217
    pobj(SL::DB::Manager::Employee->find_by(login => 'demo'));
218

  
168 219
=head2 lxinit C<login>
169 220

  
170 221
Login into lx-office using a specified login. No password will be required, and

Auch abrufbar als: Unified diff