| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 1 | $$err_str_ref .= "Directory not defined.\n"; | |||||||||||||||||||||||||||
| 33 | 1 | return; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 7 | ▶ | if ($dir eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $$err_str_ref .= "\$dir is empty string.\n"; | |||||||||||||||||||||||||||
| 37 | 0 | return; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 7 | ▶ | if (! -d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | mkdir($dir) or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | $$err_str_ref .= "Couldn't create '$dir'.\n"; | |||||||||||||||||||||||||||
| 42 | 0 | return; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 7 | ▶ | return unless @lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 || ''
MC/DC0%0/1
$leading_spaces || ''
$leading_spaces
Truth table50%1/2
A
A= $leading_spaces
| |||||||||||||||||||||||||||||
| 50 | 6 | @lines = map { s/ {$nb_leading_spaces}//; $_ } @lines; | |||||||||||||||||||||||||||
| 51 | 6 | my $i = 0; | |||||||||||||||||||||||||||
| 52 | 6 | ▶ | while ($i < @lines) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 7 | ▶ | if ($lines[$i] =~ /^\./) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 54 | 4 | _build_file($dir, $caller_pkg, $err_str_ref, $lines[$i]); | |||||||||||||||||||||||||||
| 55 | 4 | ▶ | return unless $$err_str_ref eq q[]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 63 | 3 | my @sub_lines; | |||||||||||||||||||||||||||
| 64 | 3 | ▶ | push @sub_lines, $lines[$i] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
++$i < @lines and substr($lines[$i], 0, 1) eq " "
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 " "
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $contents and $contents ne ""
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 ""
| |||||||||||||||||||||||||||||
| 84 | 4 | $hdl->close(); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # -------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 88 | 2 | 1; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||