File Coverage

blib/lib/HTML/Template/Dumper.pm
Criterion Covered Total %
statement 53 62 85.4
branch 19 28 67.8
condition 2 6 33.3
subroutine 9 10 90.0
pod 4 5 80.0
total 87 111 78.3


line stmt bran cond sub pod time code
1              
2             package HTML::Template::Dumper;
3 2     2   23748 use strict;
  2         5  
  2         79  
4 2     2   10 use warnings;
  2         4  
  2         60  
5 2     2   9 use base 'HTML::Template';
  2         4  
  2         3742  
6              
7             our $VERSION = 0.1;
8              
9             my ($format_obj, $output_filter);
10              
11             BEGIN {
12 2     2   34512 use HTML::Template::Dumper::Data_Dumper;
  2         8  
  2         87  
13 2     2   21 $format_obj = HTML::Template::Dumper::Data_Dumper->new();
14             }
15              
16              
17             sub set_output_format
18             {
19 3     3 1 11973 my $self = shift;
20 3   50     16 my $format = shift || die "Need an output format";
21 3         9 my @rest = @_;
22              
23 3         7 my $full_format = $format; # Rule 1 (see POD doc)
24 3 50       16 if($full_format !~ /::/) { # Rule 2
25 3         9 $full_format = "HTML::Template::Dumper::$format";
26             }
27              
28 3         6 $format_obj = eval {
29 3         271 eval "require $full_format";
30 3 100       26 $@ and die $@;
31 2         23 $full_format->new(@rest);
32             };
33 3 100       35 if($@) {
34             # Rule 3
35 1         4 $format_obj = eval {
36 1         60 eval "require $format";
37 1 50       34 $@ and die $@;
38 0         0 $format->new(@rest);
39             };
40             }
41              
42             # Give up trying to load the module and just attempt
43             # to call it. This would work if the module was in a
44             # package declaration placed inline with the calling
45             # file instead of a seperate file in @INC.
46             #
47 3 100       14 $@ and ($format_obj = eval { $full_format ->new(@rest) });
  1         22  
48 3 100       11 $@ and ($format_obj = eval { $format ->new(@rest) });
  1         9  
49              
50             # If we still don't have it, give up (Rule 4)
51 3 50       31 $@ and die "No such module -- $full_format";
52              
53 3 50       53 $format_obj->isa( 'HTML::Template::Dumper::Format' ) or die
54             ref $format_obj .
55             " is not a HTML::Template::Dumper::Format implementation";
56              
57 3         10 return 1;
58             }
59              
60 2     2 0 23 sub get_output_format { ref $format_obj }
61              
62             sub output
63             {
64 35     35 1 22104 my $self = shift;
65 35 100       97 my %in = @_ ? @_ : ( );
66              
67             # Call HTML::Template->output(), since it could return
68             # errors if there was a problem with the input parameters
69 35         44 eval { $self->SUPER::output(@_) };
  35         156  
70 35 50       1167 $@ and die $@;
71              
72 65         751 my $ref = {
73 35         89 map { $_ => $self->param($_) } $self->param(),
74             };
75              
76 35         752 my $output = $format_obj->dump($ref);
77 35 50       23169 $output_filter->(\$output) if $output_filter;
78              
79 35 100       84 if($in{print_to}) {
80 1         6 print {$in{print_to}} ( $output );
  1         9  
81 1         22 return undef; # As per HTML::Template docs
82             }
83 34         153 return $output;
84             }
85              
86             sub set_output_filter
87             {
88 0     0 1 0 my $self = shift;
89 0         0 my $filter = shift;
90 0 0       0 die "set_output_filter() needs to be called with a code reference"
91             unless ref $filter eq 'CODE';
92            
93 0         0 $output_filter = $filter;
94             }
95              
96             sub parse
97             {
98 2     2 1 5 my $self = shift;
99 2   50     9 my $data = shift || return;
100              
101 2 50       9 if(! ref $self ) {
102             # Called as a class method
103 0   0     0 my $format = shift || 'Data_Dumper';
104 0         0 my $dummy_tmpl = '';
105 0         0 $self = $self->new( scalarref => \$dummy_tmpl );
106 0         0 $self->set_output_format( $format );
107             }
108              
109 2         10 return $format_obj->parse( $data );
110             }
111              
112              
113             1;
114             __END__