Pen.pm untested

filter: Statementstmt 0.0% 0 / 290 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 71 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 39 totaltotal 0.0% 0 / 605 CC 58 SCAR 33.1
CC
58
Cov
0%
innerParse
90.0
parseLine
72.0
block
56.0
CRAP 27.4
1 package HTML::Pen ;
2  
3 # use 5.008009;
4 use strict;
5 use warnings;
6  
7 use FileHandle ;
8  
9 use HTML::Pen::Utils ;
10  
11 require Exporter;
12  
13 our @ISA = qw(Exporter);
14  
15 # Items to export into callers namespace by default. Note: do not export
16 # names by default without a very good reason. Use EXPORT_OK instead.
17 # Do not simply export all your public functions/methods/constants.
18  
19 # This allows declaration use Pen ':all';
20 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21 # will save memory.
22 our %EXPORT_TAGS = ( 'all' => [ qw(
23 ) ] );
24  
25 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26  
27 our @EXPORT = qw();
28  
29 our $VERSION = '1.01';
30  
31 our ( $T1, $T2, $T3 ) ;
32 our ( $pen, $DOCS, $follow ) ;
33 my ( $skipct, $crashlimit, @fh ) ;
34 my $undef = '' ;
35  
36  
37 ## request initialization
38 sub new {
39 shift @_ if $_[0] eq __PACKAGE__ ;
40 $skipct = 0 ;
41 $crashlimit = 200000 ;
42 @fh = () ;
43  
44 my $path = shift @_ unless ref $_[0] ;
45 $pen = shift ;
46 $pen ||= {} ;
47 $path ||= $pen->{request} ;
48  
49 my @path = fromFQPath( $path ) ;
50  
51 ## special variables $DOCS && $follow are historical
52 $pen->{path} ||= $path[0] ;
53 $DOCS = $pen->{path} ;
54 $pen->{request} ||= $path[1] ;
55 $follow = $pen->{request} ;
56  
57 include( $path || $pen->{request} ) ;
58 return bless {}, __PACKAGE__ ;
59 }
60  
61 sub defaultdocs {
62 return filePath( @_ ) ;
63 }
64  
65 sub filePath {
66 return join '/', @_ if $_[0] =~ m|^/| ;
67 return join '/', $pen->{path}, @_ ;
68 }
69  
70 sub fromFQPath {
71 my @path = split m|/|, pathHack( $_[0] ) ;
72 my $fn = pop @path ;
73 my $path = join '/', @path ;
74 return ( $path, $fn ) ;
75 }
76  
77 sub include {
78 my $fn = shift ;
79 my $skip = @_? shift @_: 0 ;
80  
81 ## hopefully something else barks first...
82 return if scalar @fh > 40 ;
83  
84 $fn = filePath( $fn ) ;
85  
86 my $fh = new FileHandle pathHack( $fn ) ;
87 return unless defined $fh ;
88 push @fh, $fh ;
89  
90 scalar <$fh> while ( $skip-- ) ;
91 while (<$fh>) {
92 &parseLine( $_ ) ;
93 }
94  
95 pop @fh ;
96 skip( 0 ) ;
97 return comment() ;
98 }
99  
100 sub do {
101 my $fn = shift ;
102 $fn = filePath( $fn ) ;
103 do $fn ;
104 return @_? $@ || $!: undef ;
105 }
106  
107 ## Given a symlink: ln -s /tmp/foobar foo/bar
108 ## 'foo/bar/..' may resolve to '/tmp' instead of 'foo'
109 ## pathHack explicitly performs the following conversion:
110 ## /the/quick/../brown/fox => /the/brown/fox
111  
112 sub pathHack {
113 my $path = shift ;
114  
115 my @tokens = ( '' ) ;
116 foreach ( grep $_, HTML::Pen::Utils::split( $path, '/' ) ) {
117 push @tokens, '' unless $_ eq '/' ;
118 $tokens[-1] .= $_ ;
119 }
120  
121 my @rv = () ;
122 foreach ( grep $_, @tokens ) {
123 s|//+|/|g ;
124 push @rv, $_ unless $_ eq './' || $_ eq '../' ;
125 pop @rv if $_ eq '../' ;
126 }
127  
128 return join '', @rv ;
129 }
130  
131 sub innerParse {
132 my $html = shift ;
133  
134 my @perlfun = HTML::Pen::Utils::split( $html, '&[a-zA-Z0-9_:]*\(' ) ;
135 return @perlfun if @perlfun == 1 ;
136  
137 my @tokens = grep $_, map { HTML::Pen::Utils::split( $_, '<[^<]+' ) }
138 @perlfun ;
139  
140 my @out = ( '' ) ;
141 while ( $#tokens > 1 ) {
142 last if $tokens[1] =~ /^&[a-zA-Z0-9_:]*\($/ ;
143 $out[0] .= shift @tokens ;
144 }
145
146 push @out, [ [ splice @tokens, 0, 2 ] ] ;
147 @tokens = grep $_, HTML::Pen::Utils::split( join( '', @tokens ),
148 '&[a-zA-Z0-9_:]*\(', '[\(\)>]' ) ;
149
150 tag: while ( @tokens ) {
151 for ( my $balance = 0 ; @tokens ; ) {
152 if ( $tokens[0] eq ')' && ! $balance ) {
153 shift @tokens ;
154 last ;
155 }
156
157 $balance += $tokens[0] eq '('? 1:
158 $tokens[0] eq ')'? -1: 0 ;
159 $out[1][-1][2] .= shift @tokens ;
160 }
161
162 push @{ $out[1] }, [ '' ] ;
163 unshift @tokens, '' if $tokens[0] =~ /^&[a-zA-Z0-9_:]*\($/ ;
164 while ( @tokens > 0 && $tokens[0] !~ /^&[a-zA-Z0-9_:]*\($/ ) {
165 my $t = shift @tokens ;
166 $out[1][-1][0] .= $t ;
167 last tag if $t eq '>' ;
168 }
169
170 warn "Parsing Error" unless @tokens ;
171 push @{ $out[1][-1] }, shift @tokens ;
172 }
173
174 push @out, join '', @tokens ;
175 return @out ;
176 }
177  
178 sub parseLine {
179 $T3 = shift ;
180  
181 return if $skipct-- ;
182 return unless $crashlimit-- > 0 ;
183  
184 $skipct = 0 ;
185  
186 while ( $T3 ) {
187 ( $T1, $T2, $T3 ) = innerParse( $T3 ) ;
188  
189 print $T1 ;
190 my $noembed = $T2->[0][0] =~ /<\s*:/
191 || $T2->[0][0] =~ /<!--\s*:/
192 if $T2 && ref $T2 && @$T2 ;
193  
194 foreach my $t ( @$T2 ) {
195 my @t = map { $t->[$_] || '' } ( 0..2 ) ;
196  
197 print $t[0] unless $noembed ;
198 $t[1] =~ s/^.(.*).$/$1/ if $t[1] ;
199 print evaluate( $t[1]?
200 sprintf( "&%s( %s )", @t[ 1, 2 ] ):
201 $t[2] )
202 if $t[1] || $t[2] ;
203 }
204 }
205 }
206  
207 my $evalhead =<<EOF ;
208 package HTML::Pen ;
209 no strict 'vars' ;
210 no strict 'subs' ;
211 EOF
212  
213 sub eval {
214 return evaluate( @_ ) ;
215 }
216  
217 sub evaluate {
218 my $rv = eval join "\n", $evalhead, @_ ;
219 logError( $@ ) ;
220 return $rv || $undef ;
221 }
222  
223 sub evalError {
224 my $rv = eval join "\n", $evalhead, @_ ;
225 evalError( $@ ) ;
226 return $@ || $rv || $undef ;
227 }
228  
229 sub logError {
230 my $error = shift ;
231 return unless $error && $pen->{errorlog} ;
232  
233 my $fh ;
234 open $fh, '>> ' .$pen->{errorlog} or return ;
235 print $fh $error ;
236 close $fh ;
237 return ;
238 }
239  
240 sub evalBlock {
241 evaluate( @_ ) ;
242 return undef ;
243 }
244  
245 sub evalblock {
246 return evalBlock( @_ ) ;
247 }
248  
249 sub undef {
250 evaluate( @_ ) ;
251 return undef ;
252 }
253  
254 sub comment {
255 $skipct = $_[0] *1 if @_ ;
256 return undef $T3 ;
257 }
258  
259 sub skip {
260 return comment( @_ ) ;
261 }
262  
263 sub is {
264 skip() unless $_[0] ;
265 return undef ;
266 }
267  
268 sub encode {
269 my $text = shift ;
270 $text =~ s|([^0-9A-Za-z\. ])|sprintf "%%%02X", ord($1)|seg ;
271 $text =~ s/ /+/g ;
272 return $text ;
273 }
274  
275 sub printf {
276 map { printf $T3, isRefType( $_ => 'ARRAY' )? @$_: $_ } @_ ;
277 return comment() ;
278 }
279  
280 sub isRefType {
281 my $ref = shift ;
282 my $type = shift ;
283  
284 return undef unless ref $ref ;
285 my @primitive = grep ref $ref eq $_, qw( SCALAR ARRAY HASH GLOB ) ;
286 return $primitive[0] eq $type if scalar @primitive ;
287 return $ref->isa( $type ) ;
288 }
289  
290 sub block {
291 return 'missing argument: block()' unless @_ ;
292  
293 my $ref = shift ;
294 my $blockend = shift ;
295 $blockend ||= $pen->{blockend} ;
296  
297 return 'missing end: script()' unless $blockend ;
298  
299 my $fh = $fh[ -1 ] ;
300 my $arrayref ;
301  
302 if ( ref \$ref eq 'GLOB' ) {
303 use vars qw( @glob ) ;
304 local( *glob ) = $ref ;
305 $arrayref = \@glob ;
306 }
307 elsif ( isRefType( $ref => 'ARRAY' ) ) {
308 $arrayref = $ref ;
309 }
310 elsif ( isRefType( $ref => 'SCALAR' ) ) {
311 $arrayref = $$ref = [] ;
312 }
313 else {
314 return 'bad argument: block()' ;
315 }
316  
317 @$arrayref = () ;
318 defined $fh or return undef ;
319 while ( my $line = <$fh> ) {
320 last if $line =~ /^$blockend\b/ ;
321 push( @{ $arrayref }, $line ) ;
322 }
323  
324 return comment() ;
325 }
326  
327 sub evalLine {
328 evalBlock( $T3 ) ;
329 return comment() ;
330 }
331  
332 sub evalline {
333 return evalline( @_ ) ;
334 }
335  
336 sub script {
337 my $blockend = shift ;
338 $blockend ||= $pen->{blockend} ;
339 return 'missing end: script()' unless $blockend ;
340  
341 my $block = [] ;
342 block( $block, $blockend ) ;
343 return evalBlock( @$block ) ;
344 }
345  
346 sub displayBlock {
347 map { parseLine( $_ ) } @_ ;
348 return undef ;
349 }
350  
351 sub displayblock {
352 return displayBlock( @_ ) ;
353 }
354  
355 sub loadBlock {
356 my $scalarref = isRefType( $_[0] => 'SCALAR' )? shift @_: undef ;
357 my $out ;
358  
359 my $fh = eval { return *H } ;
360 open( $fh, '>', $scalarref? $scalarref: \$out ) ;
361 my $stdout = select $fh ;
362  
363 displayBlock( @_ ) ;
364  
365 select $stdout ;
366 close $fh ;
367 return $scalarref? undef: $out ;
368 }
369  
370 sub loadblock {
371 return loadBlock( @_ ) ;
372 }
373  
374 sub mailBlock {
375 return unless $pen->{mailprogram} ;
376  
377 my $fh = eval { return *H } ;
378 open( $fh, '|', $pen->{mailprogram} ) ;
379 my $stdout = select $fh ;
380  
381 displayBlock( @_ ) ;
382  
383 select $stdout ;
384 close $fh ;
385 return undef ;
386 }
387  
388 sub mailblock {
389 return loadBlock( @_ ) ;
390 }
391  
392 ## TODO:
393 sub emailBlock {
394 # my $fh = eval { return *H } ;
395 # open $fh, "| $sendmail -t" ;
396 # my $stdout = select $fh ;
397  
398 displayBlock( @_ ) ;
399  
400 # select $stdout ;
401 # close $fh ;
402 return undef ;
403 }
404  
405 sub emailblock {
406 return emailBlock( @_ ) ;
407 }
408  
409 sub iterator {
410 my $globarg = shift ;
411 return 'missing GLOB argument: iterator()'
412 unless ref \$globarg eq 'GLOB' ;
413  
414 use vars qw( @glob $glob ) ;
415 local( *glob ) = $globarg ;
416  
417 if ( ! @_ ) {}
418 elsif ( @_ == 1 && isRefType( $_[0] => 'ARRAY' ) ) {
419 @glob = @{ $_[0] } ;
420 }
421 else {
422 @glob = @_ ;
423 }
424  
425 $glob = \@glob ;
426 return undef ;
427 }
428  
429 sub iterate {
430 my $globarg = shift ;
431 return 'missing GLOB argument: iterate()'
432 unless ref \$globarg eq 'GLOB' ;
433  
434 use vars qw( @glob $glob ) ;
435 local( *glob ) = $globarg ;
436 return undef unless isRefType( $glob => 'ARRAY' ) ;
437  
438 my @elements = @$glob ;
439 my @block = @_ == 1 && isRefType( $_[0] => 'ARRAY' )? @{ $_[0] }: @_ ;
440  
441 foreach my $element ( @elements ) {
442 foreach ( @block ) {
443 $glob = $element ;
444 parseLine( $_ ) ;
445 }
446 }
447  
448 return undef ;
449 }
450  
451 sub iteratorValue {
452 my $iterator = shift ;
453 my $key = shift ;
454 my $ref = $iterator ;
455  
456 if ( ref \$iterator eq 'GLOB' ) {
457 use vars qw( $glob ) ;
458 local( *glob ) = $iterator ;
459 $ref = $glob ;
460 }
461  
462 return iteratorValue( $ref->[0], $key )
463 if isRefType( $ref => 'ARRAY' ) ;
464 return $ref->{ $key }
465 if isRefType( $ref => 'HASH' ) ;
466 return $undef ;
467 }
468  
469 sub clear {
470 my @protected = qw( ENV ISA EXPORT EXPORT_FAIL EXPORT_TAGS EXPORT_OK
471 VERSION ) ;
472 my $cmd = "" ;
473  
474 foreach my $v ( values %HTML::Pen:: ) {
475 next if grep *{ $v }{NAME} eq $_, @protected ;
476  
477 my $scalar = *{ $v }{SCALAR} ;
478 my $name = join '', 'HTML::Pen::', *{ $v }{NAME} ;
479 next if $name =~ /::$/ ;
480  
481 $cmd .= "undef \$$name; " if defined $$scalar ;
482 $cmd .= "undef \@$name; " if defined *{ $v }{ARRAY} ;
483 $cmd .= "untie \%$name; " if defined *{ $v }{HASH}
484 && eval "tied \%Pen::$name" ;
485 $cmd .= "undef \%$name; " if defined *{ $v }{HASH} ;
486 }
487  
488 eval $cmd ;
489 return undef ;
490 }
491  
492 sub DESTROY {
493 clear() ;
494 }
495  
496 1
497 __END__