Ldraw.pm

filter: Statementstmt 15.4% 25 / 162 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 26.6% 8 / 30 Podpod 95.0% 19 / 20 totaltotal 19.7% 52 / 263 CC 30 SCAR 32.0
CC
30
Cov
12%
new_from_file
90.0
build_tree
42.0
recurse_part_tree
30.0
CRAP 24.6
↑ Summary Ldraw/Display.pm »
1 package Lego::Ldraw;
2
3 1 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings; no warnings qw/uninitialized/;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lego::Ldraw::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 use overload
13 1 ▶ '@{}' => \&lines,
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 '""' => \&stringify;
15
16
17
18 1 our $VERSION = "0.5.8";
19
20 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
21 0 my $proto = shift;
22 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
23 0 my $self = {};
24
25 0 $self->{lines} = [];
26 0 $self->{file} = undef;
27 0 $self->{name} = undef;
28 0 $self->{description} = undef;
29 0 $self->{dir} = undef;
30
31 0 bless ($self, $class);
32 0 return $self;
33 }
34
35 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
36 0 my $self = shift;
37 0 $_->{model} = undef for @{$self->lines};
38 0 $self = undef;
39 }
40
41 0 ▶sub new_from_file {
Subroutine new_from_file: not called CC=9 SCAR=45.0
Pod documented
42 0 my $self = shift->new;
43 0 ▶ my $file = shift || \*STDIN;
Condition0%0/2
shift() || \*STDIN
l!l
00
MC/DC0%0/1
shift() || \*STDIN
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
44
45 0 ▶ unless (ref $file eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file eq "GLOB") 0 0 0
46 0 ▶ croak "Error $? opening $file" unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
47 0 ▶ open LDRAW, $file or croak "Error $? opening $file";
Branch: 1 branch
BranchTrueFalseTotal
open LDRAW, $file or croak("Error $? opening $file") 0 0 0
48 0 $self->{file} = $file;
49 0 $self->{name} = basename($file);
50 0 $self->{dir} = dirname($file);
51 0 $file = \*LDRAW;
52 }
53
54 0 ▶ while (<$file>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 0 0 0
55 0 chomp;
56 0 ▶ unless (/^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s*$/) 0 0 0
57 0 my $line = Lego::Ldraw::Line->new_from_string($_);
58 0 ▶ $self->{description} = $line->command unless $.;
Branch: 1 branch
BranchTrueFalseTotal
unless $. 0 0 0
59 0 $self->add($line);
60 }
61 }
62 0 ▶ close $file unless ref $file;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
63 0 return $self;
64 }
65
66 0 ▶sub copy {
Subroutine copy: not called CC=2 SCAR=17.9
Pod documented
67 0 my $self = shift;
68 0 my $copy = Lego::Ldraw->new;
69
70 0 $copy->{file} = $self->{file};
71 0 $copy->{name} = $self->{name};
72 0 $copy->{dir} = $self->{dir};
73
74 0 for (@$self) {
75 0 my $line = Lego::Ldraw::Line->new_from_string("$_");
76 0 $copy->add($line);
77 }
78 0 return $copy;
79 }
80
81 0 ▶sub add {
Subroutine add: not called CC=3 SCAR=24.8
Pod documented
82 0 my $self = shift;
83 0 my $line = shift;
84 0 my $pos = shift;
85
86 0 ▶ $pos = 0 unless @{$self->{lines}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"lines"};} 0 0 0
87
88 0 ▶ if ($pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos) { } 0 0 0
89 0 splice @{$self->{lines}}, $pos, 0, $line;
90 } else {
91 0 push @{$self->{lines}}, $line;
92 }
93 0 $self->{tree}->{$line->part}->{$line->color}++;
94 0 $line->dir($self->dir);
95 }
96
97 0 ▶sub splice {
Subroutine splice: not called CC=4 SCAR=30.0
Pod documented
98 0 my $self = shift;
99 0 my ($what, $offset, $length) = @_;
100
101 0 for (ref $what) {
102 0 ▶ /^Lego::Ldraw::Line$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^Lego::Ldraw::Line$/ and do { CORE::splice @{$$self{"lines"};}, $offset, $length, $what; last } 0 0 0
103 0 splice @{$self->{lines}}, $offset, $length, $what;
104 0 last;
105 };
106 0 ▶ /^Lego::Ldraw$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^Lego::Ldraw$/ and do { CORE::splice @{$$self{"lines"};}, $offset, $length, @{$what->{"lines"};}; last } 0 0 0
107 0 splice @{$self->{lines}}, $offset, $length, @{$what->{lines}};
108 0 last;
109 };
110 }
111 }
112
113 0 ▶sub lines {
Subroutine lines: not called CC=2 SCAR=17.9
Pod documented
114 0 my $self = shift;
115 0 ▶ return wantarray ? @{$self->{lines}} : $self->{lines};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
116 }
117
118 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
119 0 my $self = shift;
120 0 return join "\n", @{$self};
121 }
122
123 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
124 0 my $self = shift;
125 0 return scalar @{$self->{lines}};
126 }
127
128 0 ▶sub subparts {
Subroutine subparts: not called CC=1 SCAR=6.9
Pod documented
129 0 my $self = shift;
130 0 return grep { $_->type == 1 } @{ $self };
131 }
132
133 0 ▶sub colors {
Subroutine colors: not called CC=3 SCAR=24.8
Pod documented
134 0 my $self = shift;
135 0 my %colors;
136 0 for ($self->lines) {
137 0 ▶ next unless my $c = $_->color;
Branch: 1 branch
BranchTrueFalseTotal
unless my $c = $_->color 0 0 0
138 0 $colors{$c}++;
139 }
140 0 return sort keys %colors;
141 }
142
143 ###################################################
144 # quick fixes for file, name, tree
145 ###################################################
146
147 0 ▶sub file {
Subroutine file: not called CC=1 SCAR=6.9
Pod documented
148 0 return shift->{file};
149 }
150
151 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
152 0 return shift->{name};
153 }
154
155 0 ▶sub tree {
Subroutine tree: not called CC=1 SCAR=6.9
Pod documented
156 return shift->{tree}
157 0 }
158
159 0 ▶sub parts {
Subroutine parts: not called CC=1 SCAR=6.9
Pod documented
160 0 return sort keys %{ shift->{tree} };
161 }
162
163 0 ▶sub dir {
Subroutine dir: not called CC=1 SCAR=6.9
Pod documented
164 return shift->{dir}
165 0 }
166
167 0 ▶sub description {
Subroutine description: not called CC=2 SCAR=17.9
Pod documented
168 0 my $self = shift;
169 0 ▶ $self->{description} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
170 return $self->{description}
171 0 }
172
173 0 ▶sub partsdirs {
Subroutine partsdirs: not called CC=1 SCAR=6.9
Pod documented
174 0 return Lego::Ldraw::Line->partsdirs;
175 }
176
177 0 ▶sub basedir {
Subroutine basedir: not called CC=1 SCAR=6.9
Pod documented
178 0 return Lego::Ldraw::Line->basedir;
179 }
180
181
182 ##################################################
183 # experimental part: build tree with callback
184 ##################################################
185
186 0 ▶sub Lego::Ldraw::build_tree {
Subroutine build_tree: not called CC=6 SCAR=37.4
Pod documented
187 0 my $ldraw = shift;
188 0 my $callback = shift;
189 0 my $test = shift;
190
191 0 my $b; # part tree: $d->{$part}->{$subpart} means $subpart is used in $part;
192 my $d; # reverse part tree: $d->{$subpart}->{$part} means $subpart is used in $part;
193 0 my $s; # recursed parts list. Value is 1 if list is ready to be built for subpart
194
195 0 $ldraw->recurse_part_tree(\$b, \$d, \$s, $test);
196
197 # while there are parts ready for list building
198 0 ▶ while (my @l = grep { $d->{$_} } keys %{$s}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@l) = grep({$d->{$_};} keys %{$s;})) 0 0 0
199 0 for my $p (@l) {
200 # if part has no subparts
201 0 ▶ if ($s->{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{$p}) 0 0 0
202 # build a list for the part
203 0 print STDERR "traversing tree for $p";
204 # Lego::Ldraw::Display->build_list($p);
205 0 &{$callback}($p);
206 # delete part from list of parts that need building lists
207 # delete $s->{$p}; # = 0;
208 # set parts that use it as ready for list building
209 0 my @r = keys %{$d->{$p}}; # all parts that use this subpart
210 0 for (@r) {
211 0 delete $b->{$_}->{$p}; # delete subpart from part tree
212 0 ▶ $s->{$_} = 1 unless keys %{$b->{$_}} # is there are no subparts list can be built
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$b{$_};} 0 0 0
213 }
214 # delete part reverse tree
215 0 delete $d->{$p};
216 }
217 }
218 }
219 0 my $t;
220 0 $b = undef;
221 0 $d = undef;
222 0 $s = undef;
223 }
224
225 0 ▶sub Lego::Ldraw::recurse_part_tree {
Subroutine recurse_part_tree: not called CC=5 SCAR=34.0
Pod undocumented
226 0 my $tree = shift->copy;
227 0 my ($b, $d, $s, $test) = @_;
228 0 ▶ $test = $test || sub { shift->type == 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$test || sub { (shift())->type == 1; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$test || sub { (shift())->type == 1; }
$test sub { (shift())->type == 1; }
Truth table0%0/3
A || B
A= $testB= sub { (shift())->type == 1; }
ABresult
1X1
011
000
229
230 0 $$s->{$tree->name} = 1;
231 0 for (@$tree) {
232 0 ▶ if (&{$test}($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&{$test;}($_)) 0 0 0
233 0 $$b->{$tree->name}->{$_->name}++;
234 0 $$d->{$_->name}->{$tree->name}++;
235 0 $$s->{$tree->name} = 0;
236 0 ▶ unless (defined $$s->{$_->name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$$s{$_->name}) 0 0 0
237 0 my $x = $_->explode;
238 0 $x->recurse_part_tree($b, $d, $s);
239 }
240 }
241 }
242 0 $b = undef;
243 0 $d = undef;
244 0 $s = undef;
245 0 $tree = undef;
246 }
247
248 1 1;
249 __END__
↑ Summary Ldraw/Display.pm »