HeavyCGI/Layout.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 66 CC 4 SCAR 17.0
CC
4
Cov
0%
prejoin
12.0
as_string
6.0
new
2.0
CRAP 5.5
1 package Apache::HeavyCGI::Layout;
2 use 5.005;
3  
4 use strict;
5 use vars qw($VERSION);
6  
7 #use fields qw[
8  
9 ### CONTENT
10 ### PREJOINED
11  
12 #];
13  
14 $VERSION = sprintf "%d.%03d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
15  
16 sub new {
17 my($class,@arr) = @_;
18 no strict "refs";
19 my $self = bless {}, $class;
20 $self->{CONTENT} = [@arr];
21 $self;
22 }
23  
24 sub content {
25 my Apache::HeavyCGI::Layout $self = shift;
26 @{$self->{CONTENT}};
27 }
28  
29 sub prejoin { #make the array shorter
30 my Apache::HeavyCGI::Layout $self = shift;
31 return if $self->{PREJOINED};
32 my $a = $self->{CONTENT};
33 my($i) = 0;
34 while ($i < @$a-2){
35 if ( ref($a->[$i]) || ref($a->[$i+1])){
36 ++$i;
37 } else {
38 splice @$a, $i, 2, join("",$a->[$i],$a->[$i+1]);
39 }
40 }
41 $self->{PREJOINED} = 1;
42 }
43  
44 sub as_string {
45 my Apache::HeavyCGI::Layout $self = shift;
46 my Apache::HeavyCGI $mgr = shift;
47 my @m;
48 for my $chunk ($self->content) {
49 if (ref $chunk and $chunk->can("as_string")) {
50 push @m, $chunk->as_string($mgr);
51 } else {
52 push @m, "$chunk";
53 # Carp::cluck("Hey-4. chunk[$chunk]");
54 }
55 }
56 join "", @m;
57 }
58  
59 1;
60  
61 =head1 NAME
62  
63 Apache::HeavyCGI::Layout - Represent a page layout in an array
64  
65 =head1 SYNOPSIS
66  
67 my $layout = Apache::HeavyCGI::Layout->new(@array);
68  
69 $layout->prejoin; # make the array more compact
70  
71 my @array = $layout->content;
72 my $string = $layout->as_string($object);
73  
74 =head1 DESCRIPTION
75  
76 The constructor new() takes as an argument an array of elements.
77 Elements may be strings and objects in any order.
78  
79 The content() method returns the array of elements.
80  
81 The prejoin() method joins adjacent string elements, leaving at most
82 one string element between objects in the array.
83  
84 The as_string() method takes an object, say $mgr, as an argument and
85 joins all elements of the array such that all chunks that represent an
86 object are called with
87  
88 $chunk->as_string($mgr)
89  
90 and all chunks that represent a string are fillers in between. Objects
91 that do not understand the as_string method are just filled in as
92 strings, leaving room for debugging or overloading or whatever.
93  
94 =cut
95