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