silent warnings from Data::Dumper about dummy CODE refs
[catagits/Catalyst-Runtime.git] / t / lib / TestApp / View / Dump.pm
1 package TestApp::View::Dump;
2
3 use strict;
4 use base 'Catalyst::View';
5
6 use Data::Dumper ();
7 use Scalar::Util qw(blessed weaken);
8
9 sub dump {
10     my ( $self, $reference, $purity ) = @_;
11
12     return unless $reference;
13
14     $purity = defined $purity ? $purity : 1;
15
16     my $dumper = Data::Dumper->new( [$reference] );
17     $dumper->Indent(1);
18     $dumper->Purity($purity);
19     $dumper->Useqq(0);
20     $dumper->Deepcopy(1);
21     $dumper->Quotekeys(0);
22     $dumper->Terse(1);
23
24     local $SIG{ __WARN__ } = sub { warn unless $_[ 0 ] =~ m{dummy} };
25     return $dumper->Dump;
26 }
27
28 sub process {
29     my ( $self, $c, $reference, $purity ) = @_;
30
31     # Force processing of on-demand data
32     $c->prepare_body;
33
34     # Remove body from reference if needed
35     $reference->{__body_type} = blessed $reference->body
36         if (blessed $reference->{_body});
37     my $body = delete $reference->{_body};
38
39     # Remove context from reference if needed
40     my $context = delete $reference->{_context};
41
42     if ( my $output =
43         $self->dump( $reference, $purity ) )
44     {
45
46         $c->res->headers->content_type('text/plain');
47         $c->res->output($output);
48
49         if ($context) {
50             # Repair context
51             $reference->{_context} = $context;
52             weaken( $reference->{_context} );
53         }
54
55         if ($body) {
56             # Repair body
57             delete $reference->{__body_type};
58             $reference->{_body} = $body;
59         }
60
61         return 1;
62     }
63
64     return 0;
65 }
66
67 1;