TreeBuilder.pm

filter: Statementstmt 93.8% 76 / 81 Branchbran 85.7% 24 / 28 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 90.5% 124 / 137 CC 18 SCAR 20.3
CC
18
Cov
91%
_build_tree
14.7
_build_file
4.0
build_tree
2.0
CRAP 7.6
↑ Summary TreeBuilder_T.pm »
1 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package File::TreeBuilder;
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $File::TreeBuilder::VERSION = '0.02';
7 }
8 # ABSTRACT: Build simple trees of files and directories.
9  
10 # --------------------------------------------------------------------
11 2 require Exporter;
12 2 our @ISA = qw(Exporter);
13 2 our @EXPORT_OK = qw(build_tree);
14  
15 # --------------------------------------------------------------------
16 5 ▶sub build_tree {
Subroutine build_tree: called CC=2 SCAR=6.9
Pod documented
17 5 my ($dir, $str) = @_;
18 5 my $caller_pkg = (caller)[0];
19 5 ▶ $str = q[] unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 2 3 5
20 5 my @lines = split /\n/, $str;
21 # Remove blank lines and comments.
22 5 @lines = grep ! /^\s*(?:#|$)/, @lines;
23 5 my $err_str = q[];
24 5 _build_tree($dir, $caller_pkg, \$err_str, @lines);
25 5 return $err_str;
26 }
27  
28 # --------------------------------------------------------------------
29 8 ▶sub _build_tree {
Subroutine _build_tree: called CC=14 SCAR=26.8
30 8 my ($dir, $caller_pkg, $err_str_ref, @lines) = @_;
31 8 ▶ if (! defined $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 1 7 8
32 1 $$err_str_ref .= "Directory not defined.\n";
33 1 return;
34 }
35 7 ▶ if ($dir eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "") 0 7 7
36 0 $$err_str_ref .= "\$dir is empty string.\n";
37 0 return;
38 }
39 7 ▶ if (! -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 3 4 7
40 3 ▶ mkdir($dir) or do {
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or do { $$err_str_ref .= "Couldn't create '${dir}'.\n"; return } 3 0 3
41 0 $$err_str_ref .= "Couldn't create '$dir'.\n";
42 0 return;
43 }
44 }
45 7 ▶ return unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 1 6 7
46 # Remove from the beginning of each line as many leading
47 # spaces as there are on the first one.
48 6 my ($leading_spaces) = $lines[0] =~ /^( +)/;
49 6 ▶ my ($nb_leading_spaces) = length($leading_spaces || '');
Condition50%1/2
$leading_spaces || ''
l!l
60
MC/DC0%0/1
$leading_spaces || ''
$leading_spaces
Truth table50%1/2
A
A= $leading_spaces
Aresult
11
00
50 6 @lines = map { s/ {$nb_leading_spaces}//; $_ } @lines;
51 6 my $i = 0;
52 6 ▶ while ($i < @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 7 5 12
53 7 ▶ if ($lines[$i] =~ /^\./) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lines[$i] =~ /^\./) { } 4 3 7
elsif ($lines[$i] =~ m[^/]) { } 3 0 3
54 4 _build_file($dir, $caller_pkg, $err_str_ref, $lines[$i]);
55 4 ▶ return unless $$err_str_ref eq q[];
Branch: 1 branch
BranchTrueFalseTotal
unless $$err_str_ref eq "" 1 3 4
56 3 ++$i;
57 }
58 elsif ($lines[$i] =~ /^\//) {
59 3 my ($to_eval) = $lines[$i] =~ / ^ \/ \s+ (.*) /x;
60 3 my $sub_dir;
61 3 ▶ eval "no strict; package $caller_pkg; (\$sub_dir) = ($to_eval)";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
62 3 ▶ ++$i, next unless defined $sub_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub_dir 0 3 3
63 3 my @sub_lines;
64 3 ▶ push @sub_lines, $lines[$i]
Branch: 1 branch
BranchTrueFalseTotal
++$i < @lines and substr($lines[$i], 0, 1) eq " " and do { push @sub_lines, $lines[$i] } 3 3 6
Condition100%3/3
++$i < @lines and substr($lines[$i], 0, 1) eq " "
!ll&&!rl&&r
213
MC/DC100%2/2
++$i < @lines and substr($lines[$i], 0, 1) eq " "
++$i < @lines substr($lines[$i], 0, 1) eq " "
Truth table100%3/3
A and B
A= ++$i < @linesB= substr($lines[$i], 0, 1) eq " "
ABresult
0X0
100
111
65 while ++$i < @lines && substr($lines[$i], 0, 1) eq ' ';
66 3 _build_tree("$dir/$sub_dir", $caller_pkg, $err_str_ref, @sub_lines);
67 }
68 else {
69 0 ++$i;
70 }
71 }
72 }
73  
74 # --------------------------------------------------------------------
75 4 ▶sub _build_file {
Subroutine _build_file: called CC=4 SCAR=13.9
76 4 my ($dir, $caller_pkg, $err_str_ref, $line) = @_;
77 4 my ($to_eval) = $line =~ / ^ \. \s+ (.*) /x;
78 4 my ($fname, $contents);
79 4 ▶ eval "no strict; package $caller_pkg; (\$fname, \$contents) = ($to_eval)";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
80 4 my $file_spec = "$dir/$fname";
81 4 ▶ open my $hdl, ">", $file_spec
Branch: 1 branch
BranchTrueFalseTotal
open my $hdl, ">", $file_spec or $$err_str_ref .= "Couldn't open '${file_spec}' for writing: $!" 3 1 4
82 or $$err_str_ref .= "Couldn't open '$file_spec' for writing: $!";
83 4 ▶ print $hdl $contents if defined($contents) && $contents ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $contents and $contents ne "" 1 3 4
Condition66%2/3
defined $contents and $contents ne ""
!ll&&!rl&&r
301
MC/DC50%1/2
defined $contents and $contents ne ""
defined $contents $contents ne ""
Truth table66%2/3
A and B
A= defined $contentsB= $contents ne ""
ABresult
0X0
100
111
84 4 $hdl->close();
85 }
86  
87 # --------------------------------------------------------------------
88 2 1;
89  
90 __END__
↑ Summary TreeBuilder_T.pm »