blib/lib/HTML/Myasp.pm

filter: Statementstmt 21.9% 25 / 114 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 3 totaltotal 14.2% 30 / 211 CC 44 SCAR 73.9
CC
44
Cov
13%
send_page
1880.5
add_sub_entry
2.0
add_replace_entry
2.0
CRAP 1622.5
↑ Summary
1 package HTML::Myasp;
2  
3 1 require Exporter;
4  
5 1 @ISA = qw(Exporter);
6 1 @EXPORT = qw/send_page/;
7  
8 1 require 5.005_62;
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.05';
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 my %CACHE;
18 1 our $DEBUG = 0;
19  
20 1 our %g_hparam;
21 1 our %g_hreplace;
22  
23 #-----------------------------------------------------------------------------------------------
24 0 ▶sub add_sub_entry {
Subroutine add_sub_entry: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 0 %g_hparam = (%g_hparam, @_);
27  
28 }
29  
30 #-----------------------------------------------------------------------------------------------
31 # add an entry in the direct replace hash (the second)
32 0 ▶sub add_replace_entry {
Subroutine add_replace_entry: not called CC=1 SCAR=6.9
Pod undocumented
33  
34 0 %g_hreplace = (%g_hreplace, @_);
35  
36 }
37  
38 #-----------------------------------------------------------------------------------------------
39 0 ▶sub send_page {
Subroutine send_page: not called CC=44 SCAR=75.4
Pod undocumented
40  
41 0 local $SIG{__DIE__} = \&Carp::confess;
42  
43 0 my ($file, $hparam, $hreplace) = @_;
44  
45 0 map {$hparam->{$_} = $g_hparam{$_} } keys %g_hparam;
46 0 map {$hreplace->{$_} = $g_hreplace{$_} } keys %g_hreplace;
47  
48 0 ▶ print STDERR "Sub Routine reference hash is\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
49 0 ▶ warn Dumper $hparam if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
50 0 ▶ print STDERR "Direct replacing hash\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
51 0 ▶ warn Dumper $hreplace if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
52  
53 0 my $tagprefix;
54  
55 0 my ($source, $string_text);
56 0 ▶ if (length($file) > 128) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $file > 128) { } 0 0 0
57 0 $source = 'string';
58 0 $string_text = $file;
59 0 $file = substr($file,0,255);
60 } else {
61 0 $source = 'file';
62 }
63  
64 0 ▶ warn "Source is coming from a $source" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
65  
66 0 ▶ if (exists $ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'MOD_PERL'}) { } 0 0 0
67 0 my $r = Apache->request();
68 0 $file = $r->document_root() . "/$file"; # if mod_perl the document root is prefixed to the file path
69 0 ▶ $tagprefix = $r->dir_config("TagPrefix") || 'myasp';
Condition0%0/2
$r->dir_config('TagPrefix') || 'myasp'
l!l
00
MC/DC0%0/1
$r->dir_config('TagPrefix') || 'myasp'
$r->dir_config('TagPrefix')
Truth table0%0/2
A
A= $r->dir_config('TagPrefix')
Aresult
11
00
70 } else {
71 0 $tagprefix = 'myasp';
72 }
73  
74 0 ▶ $tagprefix = $ENV{TagPrefix} if $ENV{TagPrefix}; # always environment variables can change behavior
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TagPrefix"} 0 0 0
75  
76 0 ▶ warn "TagPrefix is $tagprefix" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
77  
78 0 ▶ my $mtime = $source eq 'file' ? (stat($file))[9] : time;
Branch: 1 branch
BranchTrueFalseTotal
$source eq 'file' ? : 0 0 0
79  
80 0 my ($package,$filename,$line) = caller;
81 0 my $caller_mtime = (stat($filename))[9];
82  
83 0 ▶ if (!$CACHE{$file} || $mtime > $CACHE{$file}->{load_time} || $caller_mtime > $CACHE{$file}->{caller_mtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $CACHE{$file} or $mtime > $CACHE{$file}{'load_time'} or $caller_mtime > $CACHE{$file}{'caller_mtime'}) { } 0 0 0
Condition0%0/6
not $CACHE{$file} or $mtime > $CACHE{$file}{"load_time"} or $caller_mtime > $CACHE{$file}{"caller_mtime"}
l!l&&r!l&&!r
000
not $CACHE{$file} or $mtime > $CACHE{$file}{"load_time"}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $CACHE{$file} or $mtime > $CACHE{$file}{"load_time"} or $caller_mtime > $CACHE{$file}{"caller_mtime"}
not $CACHE{$file} $mtime > $CACHE{$file}{"load_time"} $caller_mtime > $CACHE{$file}{"caller_mtime"}
Truth table0%0/4
not(A) or B or C
A= not $CACHE{$file}B= $mtime > $CACHE{$file}{"load_time"}C= $caller_mtime > $CACHE{$file}{"caller_mtime"}
ABCresult
1XX1
01X1
0011
0000
84  
85 0 ▶ warn "File $file reloaded or loaded first time" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
86  
87 0 ▶ if ($source eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq 'file') { } 0 0 0
88 1 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 0 my $fh = new IO::File;
90 0 ▶ unless ($fh->open("<$file")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->open("<$file")) 0 0 0
91 0 print "<p>Archivo no encontrado: $file</p>";
92 0 return;
93 }
94 0 local $/ = undef;
95 0 $CACHE{$file}->{f_text} = <$fh>;
96 0 close $fh;
97 } else {
98 0 $CACHE{$file}->{f_text} = $string_text;
99 }
100  
101 0 $CACHE{$file}->{load_time} = time;
102 0 $CACHE{$file}->{caller_mtime} = time;
103
104  
105 0 my ($last, @data);
106  
107 0 ▶ while ($CACHE{$file}->{f_text} =~ m#(.+?)<$tagprefix:(.+?)(\s.+?)?>(.*?)</$tagprefix:\2>#gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CACHE{$file}{"f_text"} =~ m[(.+?)<$tagprefix:(.+?)(\s.+?)?>(.*?)</$tagprefix:\2>]gs) 0 0 0
108  
109 0 ▶ warn "$2 mark found" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
110  
111 0 my %arr;
112 0 ▶ if ($3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($3) 0 0 0
113 0 my $aux = $3;
114 0 $aux =~ s/^\s+//; $aux =~ s/\s+$//;
115  
116 # while (my ($key, $value) = $aux =~ /(\S+)=("[^"]"|\S+)/gs) {
117 # $arr{$key} = $value;
118 # }
119  
120 0 %arr = split /=|\s+/, $aux;
121  
122 0 foreach (keys %arr) {
123 0 $arr{$_} =~ s/"//g;
124 }
125 }
126 0 push @data, {-html => $1, -type=>'html'};
127 0 ▶ push @data, {-name=>"-$2", -coderef => $hparam->{"\-$2"}, -type=>'cod', -param_ref => \%arr, -param_body => $4} if $hparam->{"\-$2"};
Branch: 1 branch
BranchTrueFalseTotal
if $$hparam{"-$2"} 0 0 0
128  
129 0 $last = $';
130
131 }
132 0 ▶ if (@data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) { } 0 0 0
133 0 push @data, {-html => $last, -type=>'html'};
134 } else {
135 0 push @data, {-html =>$CACHE{$file}->{f_text}, -type=>'html'};
136 }
137  
138 0 $CACHE{$file}->{data} = \@data;
139  
140 } else {
141 0 ▶ warn "Cache Hit file $file" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
142 }
143  
144  
145 # the tags fields in the page are
146  
147 0 ▶ print STDERR "Begin parsed data file ***********\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
148 0 ▶ warn Dumper $CACHE{$file} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
149 0 ▶ print STDERR "End parsed data file ***********\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
150  
151 0 foreach my $x (@{$CACHE{$file}->{data}}) {
152 0 ▶ if ($x->{-type} eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'-type'} eq 'html') { } 0 0 0
153 0 ▶ next unless $x->{-html};
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"-html"} 0 0 0
154 0 my $temp = $x->{-html};
155 0 ▶ print STDERR "Checking HTML replacing keywords\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
156 0 ▶ warn $temp if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
157  
158 0 foreach my $key (keys %$hreplace) {
159 0 ▶ warn "Checking key \"${key}\" and replacing with \"$hreplace->{$key}\"" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
160
161 0 ▶ if ($temp =~ s/\b${key}\b/$hreplace->{$key}/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp =~ s/\b$key\b/$hreplace->{$key}/sig) 0 0 0
162 0 ▶ warn "String $key replaced" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
163 }
164 }
165 0 print $temp;
166 } else {
167  
168 0 ▶ warn "Calling function $x->{-name}" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
169 0 ▶ &Carp::cluck ("Stack de llamadas") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
170 0 ▶ print STDERR "The atributes of the tag are\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
171 0 ▶ warn Dumper $x->{-param_ref} if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
172 0 ▶ print STDERR "*** Begin body between the marks is \n$x->{-param_body}\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
173 0 ▶ print STDERR "*** End body between the marks\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Myasp::DEBUG 0 0 0
174  
175 0 ▶ unless (ref($hparam->{$x->{-name}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$hparam{$x->{'-name'}}) { } 0 0 0
176 0 warn "Tag in template but not a handler subroutine defined: key $x->{-name}, template $file, called from $filename line $line";
177 0 next;
178 } else {
179 0 &{$hparam->{$x->{-name}}}($x->{-param_ref}, $x->{-param_body});
180 }
181 }
182
183 }
184  
185 0 1;
186  
187 }
188  
189  
190  
191  
192 1 1;
193 __END__
↑ Summary