File Coverage

blib/lib/CGI/Application/Plugin/AnyTemplate/ComponentHandler.pm
Criterion Covered Total %
statement 41 44 93.1
branch 2 4 50.0
condition 1 3 33.3
subroutine 9 10 90.0
pod 3 5 60.0
total 56 66 84.8


line stmt bran cond sub pod time code
1              
2             package CGI::Application::Plugin::AnyTemplate::ComponentHandler;
3              
4 24     24   25207 use CGI::Application; # fix for older version of CAP::Forward so
  24         7930  
  24         726  
5             # that it can install its hooks properly
6 24     24   23405 use CGI::Application::Plugin::Forward;
  24         15810  
  24         1325  
7              
8             =head1 NAME
9              
10             CGI::Application::Plugin::AnyTemplate::ComponentHandler - Embed run modes within a template
11              
12             =head1 DESCRIPTION
13              
14             This is a little helper module used by
15             L to handle finding and running
16             the run modes for embedded components, and returning their content.
17              
18             You shouldn't need to use this module directly unless you are adding
19             support for a new template system.
20              
21             For information on embedded components see the docs of
22             L.
23              
24             =cut
25              
26 24     24   147 use strict;
  24         50  
  24         635  
27 24     24   121 use Carp;
  24         46  
  24         1425  
28 24     24   136 use Scalar::Util qw(weaken);
  24         45  
  24         9921  
29              
30             =head1 METHODS
31              
32             =over 4
33              
34             =item new
35              
36             Creates a new C object.
37              
38             my $component_handler = CGI::Application::Plugin::AnyTemplate::ComponentHandler->new(
39             webapp => $webapp,
40             containing_template => $template,
41             );
42              
43             The C parameter should be a reference to a C
44             object.
45              
46             The C parameter should be a reference to the template
47             object in which this component is embedded.
48              
49             =cut
50              
51              
52             sub new {
53 95     95 1 180 my $proto = shift;
54 95   33     462 my $class = ref $proto || $proto;
55              
56 95         335 my %args = @_;
57              
58 95         188 my $self = {};
59 95         322 bless $self, $class;
60              
61 95         321 $self->{'webapp'} = $args{'webapp'};
62 95         225 $self->{'containing_template'} = $args{'containing_template'};
63              
64 95         305 weaken $self->{'webapp'};
65 95         291 weaken $self->{'containing_template'};
66              
67 95         380 return $self;
68             }
69              
70             =item embed
71              
72             Runs the specified C of the C object.
73             Returns the results of this call.
74              
75             Parameters passed to embed should be passed on to the run mode.
76              
77             If the results are a scalar reference, then the return value is
78             dereferenced before returning. This is the safest way of calling a run
79             mode since you'll get the output as a string and return it as a string,
80             but it involves returning potentially very large strings from
81             subroutines.
82              
83             =cut
84              
85             sub embed {
86 11     11 1 283969 my $self = shift;
87 11         32 my $run_mode_name = shift;
88              
89 11         37 my $webapp = $self->{'webapp'};
90 11         28 my $containing_template = $self->{'containing_template'};
91              
92 11         20 my $output;
93 11         23 eval {
94 11         285 $output = $webapp->CGI::Application::Plugin::Forward::forward($run_mode_name, $containing_template, @_);
95             };
96 11 50       150 if ($@) {
97 0         0 confess("Error embedding run mode [$run_mode_name] in web app [$webapp]: $@\n");
98             }
99              
100 11 50       206 if (ref $output eq 'SCALAR') {
101 11         72 return $$output;
102             }
103             else {
104 0         0 return $output;
105             }
106             }
107              
108             sub dispatch {
109 1     1 0 50476 goto &embed;
110             }
111              
112              
113             =item embed_direct
114              
115             Runs the specified C of the C object.
116             Returns the results of this call.
117              
118             Parameters passed to embed_direct should be passed on to the run mode.
119              
120             Even if the result of this call is a scalar reference, the result
121             is NOT dereferenced before returning it.
122              
123             If you call this method instead of embed, you should be careful to deal
124             with the possibility that your results are a reference to a string and
125             not the string itself.
126              
127             =back
128              
129             =cut
130              
131             sub embed_direct {
132 11     11 1 47 my $self = shift;
133 11         17 my $run_mode_name = shift;
134              
135 11         18 my $webapp = $self->{'webapp'};
136 11         18 my $containing_template = $self->{'containing_template'};
137              
138             # I'd like to have some error handling here, but wrapping this in
139             # an eval makes return stop working :(
140 11         79 return $webapp->CGI::Application::Plugin::Forward::forward($run_mode_name, $containing_template, @_);
141             }
142              
143             sub dispatch_direct {
144 0     0 0   goto &embed_direct;
145             }
146              
147             =head1 AUTHOR
148              
149             Michael Graham, C<< >>
150              
151             =head1 COPYRIGHT & LICENSE
152              
153             Copyright 2005 Michael Graham, All Rights Reserved.
154              
155             This program is free software; you can redistribute it and/or modify it
156             under the same terms as Perl itself.
157              
158             =cut
159              
160             1;
161              
162