Skip to content

Instantly share code, notes, and snippets.

@oalders
Created August 13, 2015 19:03
Show Gist options
  • Save oalders/98b55d5cebbeac07080b to your computer and use it in GitHub Desktop.
Save oalders/98b55d5cebbeac07080b to your computer and use it in GitHub Desktop.
package Plack::Middleware::Debug::Session::Pretty;
use strict;
use warnings;
use parent qw(Plack::Middleware::Debug::Base);
use Data::Printer 0.36;
use Text::MicroTemplate;
my $list_template = __PACKAGE__->build_template(<<'EOTMPL');
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
% my $i;
% while (@{$_[0]->{list}}) {
% my($key, $value) = splice(@{$_[0]->{list}}, 0, 2);
<tr class="<%= ++$i % 2 ? 'plDebugOdd' : 'plDebugEven' %>">
<td><%= $key %></td>
<td><pre><%= vardump($value) %></pre></td>
</tr>
% }
</tbody>
</table>
EOTMPL
sub panel_name {
return 'Session::Pretty';
}
sub vardump {
my $scalar = shift;
return '(undef)' unless defined $scalar;
return "$scalar" unless ref $scalar;
return np($scalar);
}
sub run {
my ( $self, $env, $panel ) = @_;
return sub {
my $session = $env->{'psgix.session'} or return $panel->disable;
return $panel->content(
$list_template->(
{
list => [
map { $_ => $session->{$_} }
sort keys %{$session}
]
}
)
);
};
}
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment