blib/lib/Text/Yats.pm

filter: Statementstmt 100% 79 / 79 Branchbran 95.8% 23 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 9 / 9 Podpod 57.1% 4 / 7 totaltotal 93.0% 120 / 129 CC 18 SCAR 17.0
CC
18
Cov
97%
replace
9.0
new
6.0
make_array
4.0
CRAP 5.5
↑ Summary
1 #
2 # Yats.pm
3 # Last Modification: 2002/01/07 (hdias@esb.ucp.pt)
4 #
5 # Copyright (c) 2001 Henrique Dias. All rights reserved.
6 # This module is free software; you can redistribute it and/or modify
7 # it under the same terms as Perl itself.
8 #
9 #
10  
11 package Text::Yats;
12
13 5 require 5;
14 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 require Exporter;
17 5 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 @ISA = qw(Exporter DynaLoader);
19 5 $VERSION = '0.03';
20  
21 16 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
22 16 my $proto = shift;
23 16 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1240
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
24 16 my $self = {
25 section => [],
26 level => 0,
27 file => "",
28 text => "",
29 base => '\\d+',
30 pattern => '\\d+',
31 @_,
32 };
33 16 bless ($self, $class);
34  
35 16 ▶ $self->{text} = &get_text($self->{'file'}) if($self->{file});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 4 12 16
36 16 ▶ my $sections = ($self->{level} > 0) ? $self->wrapper() : [];
Branch: 1 branch
BranchTrueFalseTotal
$self->{'level'} > 0 ? : 6 10 16
37 16 ▶ if($#{$sections} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$sections;} > 0) 4 12 16
38 4 $self->{level}--;
39 4 $self->{pattern} .= '\\.' . $self->{base};
40 4 for(0 .. $#{$sections}) {
41 $self->{section}->[$_] = $self->new(
42 'level' => $self->{level},
43 'pattern' => $self->{pattern},
44 12 'text' => $sections->[$_]);
45 }
46 }
47 16 return($self);
48 }
49  
50 14 ▶sub section { $_[0]->{'section'}; }
Subroutine section: called CC=1 SCAR=0.0
Pod documented
51  
52 9 ▶sub replace {
Subroutine replace: called CC=9 SCAR=22.0
Pod documented
53 9 my $self = shift;
54 9 my $param = {@_};
55  
56 9 my $text = "";
57 9 my $max = 0;
58 9 my $i = 0;
59 9 my $pattern = '\$(\w+) *<!--\(((?:(?!\)-->).)+)\)-->';
60 9 LOOP: while(1) {
61 20 my $tmp = $self->{text};
62 20 ▶ while($tmp =~ s/$pattern/\$$1/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ s/$pattern/\$$1/o) 1 20 21
63 1 $self->{text} = $tmp;
64 1 $param->{$1} = [eval($2)];
65 }
66 20 for(keys(%{$param})) {
67 $param->{$_} = &make_array($param->{$param->{$_}->{array}},$param->{$_}->{match},$param->{$_}->{value})
68 54 ▶ if(ref($param->{$_}) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if ref $param->{$_} eq "HASH" 5 49 54
69 54 ▶ if(ref($param->{$_}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param->{$_} eq 'ARRAY') { } 42 12 54
70 42 my $maxtmp = $#{$param->{$_}};
71 42 ▶ $max = $maxtmp unless($maxtmp <= $max);
Branch: 1 branch
BranchTrueFalseTotal
unless $maxtmp <= $max 5 37 42
72 42 ▶ if(($i <= $maxtmp) && ($param->{$_}->[$i] ne "")) { $tmp =~ s/\$$_\b/$param->{$_}->[$i]/g; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $maxtmp and $param->{$_}[$i] ne '') { } 32 10 42
Condition66%2/3
$i <= $maxtmp and $param->{$_}[$i] ne ""
!ll&&!rl&&r
01032
MC/DC50%1/2
$i <= $maxtmp and $param->{$_}[$i] ne ""
$i <= $maxtmp $param->{$_}[$i] ne ""
Truth table66%2/3
A and B
A= $i <= $maxtmpB= $param->{$_}[$i] ne ""
ABresult
0X0
100
111
73 10 else { $tmp =~ s/ ?\$$_\b//g; }
74 12 } else { $tmp =~ s/\$$_\b/$param->{$_}/g; }
75 }
76 20 $text .= $tmp;
77 20 ▶ last LOOP if($i == $max);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $max 9 11 20
78 11 $i++;
79 }
80 9 return($text);
81 }
82  
83 5 ▶sub make_array {
Subroutine make_array: called CC=4 SCAR=13.9
Pod undocumented
84 5 my ($array, $match, $value) = @_;
85  
86 5 my @matched = ();
87 5 $#matched = $#{$array};
88 5 my %keys = ();
89 5 ▶ if(ref($match) eq "ARRAY") { @keys{@{$match}} = (); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match eq 'ARRAY') { } 1 4 5
90 4 else { $keys{$match} = ""; }
91 5 for my $j (0 .. $#{$array}) {
92 16 ▶ $matched[$j] = (exists($keys{$array->[$j]})) ? $value : "";
Branch: 1 branch
BranchTrueFalseTotal
exists $keys{$array->[$j]} ? : 6 10 16
93 }
94 5 return(\@matched);
95 }
96  
97 3 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
98 3 my $self = shift;
99 3 return($self->{text});
100 }
101  
102 6 ▶sub wrapper {
Subroutine wrapper: called CC=1 SCAR=0.0
Pod undocumented
103 6 my $self = shift;
104 6 my $pattern = '<!--{' . $self->{pattern} . '}-->\n*';
105 6 my $re = qr/$pattern/;
106 6 my @sections = split(/$re/, $self->{text});
107 6 return(\@sections);
108 }
109  
110 4 ▶sub get_text {
Subroutine get_text: called CC=2 SCAR=7.0
Pod undocumented
111 4 my $filename = shift;
112  
113 4 local $/ = undef;
114 4 local *FILE;
115 4 ▶ open (FILE, "<$filename") || die "Can't open $filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$filename" or die "Can't open ${filename}: $!\n" 4 0 4
116 4 my $text = <FILE>;
117 4 close(FILE);
118 4 return($text);
119 }
120  
121 5 1;
122 __END__
↑ Summary