blib/lib/File/Slurp/Tree.pm

filter: Statementstmt 100% 46 / 46 Branchbran 83.3% 10 / 12 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 87.6% 71 / 81 CC 12 SCAR 17.2
CC
12
Cov
91%
slurp_tree
9.3
spew_tree
4.0
BEGIN
1.0
CRAP 5.6
↑ Summary
1 package File::Slurp::Tree;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use File::Path qw( mkpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Slurp qw( read_file write_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 require Exporter;
7 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use vars qw(@EXPORT $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 @EXPORT = qw( slurp_tree spew_tree );
11 2 $VERSION = 1.24;
12  
13 =head1 NAME
14  
15 File::Slurp::Tree - slurp and emit file trees as nested hashes
16  
17 =head1 SYNOPSIS
18  
19 # (inefficiently) duplicate a file tree from path a to b
20 use File::Slurp::Tree;
21 my $tree = slurp_tree( "path_a" );
22 spew_tree( "path_b" => $tree );
23  
24 =head1 DESCRIPTION
25  
26 File::Slurp::Tree provides functions for slurping and emitting trees
27 of files and directories.
28  
29 # an example of use in a test suite
30 use Test::More tests => 1;
31 use File::Slurp::Tree;
32 is_deeply( slurp_tree( "t/some_path" ), { foo => {}, bar => "sample\n" },
33 "some_path contains a directory called foo, and a file bar" );
34  
35 The tree datastructure is a hash of hashes. The keys of each hash are
36 names of directories or files. Directories have hash references as
37 their value, files have a scalar which holds the contents of the file.
38  
39 =head1 EXPORTED ROUTINES
40  
41 =head2 slurp_tree( $path, %options )
42  
43 return a nested hash reference containing everything within $path
44  
45 %options may include the following keys:
46  
47 =over
48  
49 =item rule
50  
51 a L<File::Find::Rule> object that will match the files and directories
52 in the path. defaults to an empty rule (matches everything)
53  
54 =back
55  
56 =cut
57  
58 2 ▶sub slurp_tree {
Subroutine slurp_tree: called CC=9 SCAR=22.3
Pod documented
59 2 my $in = shift;
60 2 my %args = @_;
61  
62 # top must not have a trailing slash, in may. this fixes Greg's bug
63 # and allows in to be "/"
64 2 (my $top = $in) =~ s{/$}{};
65  
66 2 ▶ my $rule = $args{rule} || File::Find::Rule->new;
Condition33%1/3
$args{'rule'} || 'File::Find::Rule'->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$args{'rule'} || 'File::Find::Rule'->new
$args{'rule'} 'File::Find::Rule'->new
Truth table33%1/3
A || B
A= $args{'rule'}B= 'File::Find::Rule'->new
ABresult
1X1
011
000
67 2 my $tree = {};
68 2 for my $file ( $rule->in( $in ) ) {
69 6 ▶ next if $file eq $top;
Branch: 1 branch
BranchTrueFalseTotal
if $file eq $top 2 4 6
70 4 (my $rel = $file) =~ s{^\Q$top\E/}{};
71 4 ▶ next unless $rel; # it's /
Branch: 1 branch
BranchTrueFalseTotal
unless $rel 0 4 4
72 #print "top:$top file:$file rel:$rel\n";
73  
74 4 my @elems = split m{/}, $rel;
75  
76 # go to the top of the tree
77 4 my $node = $tree;
78 # and walk along the path
79 4 ▶ while (my $elem = shift @elems) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $elem = shift @elems) 4 4 8
80 # on the path || a dir
81 4 ▶ if (@elems || -d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elems or -d $file) { } 2 2 4
Condition66%2/3
@elems or -d $file
l!l&&r!l&&!r
022
MC/DC50%1/2
@elems or -d $file
@elems -d $file
Truth table66%2/3
A or B
A= @elemsB= -d $file
ABresult
1X1
011
000
82 2 ▶ $node = $node->{ $elem } ||= {};
Condition50%1/2
$node->{$elem} ||= {}
l!l
02
MC/DC0%0/1
$node->{$elem} ||= {}
$node->{$elem}
Truth table50%1/2
A
A= $node->{$elem}
Aresult
11
00
83 }
84 else {
85 # a file, slurp it
86 2 $node->{ $elem } = read_file "$file", binmode => ':raw';
87 }
88 }
89 }
90 2 return $tree;
91 }
92  
93 =head2 spew_tree( $path => $tree )
94  
95 Creates a file tree as described by C<$tree> at C<$path>
96  
97 =cut
98  
99 2 ▶sub spew_tree {
Subroutine spew_tree: called CC=4 SCAR=13.9
Pod documented
100 2 my ($top, $tree) = @_;
101 2 eval { mkpath( $top ) };
102 2 for my $stem (keys %$tree) {
103 2 ▶ if (ref $tree->{$stem}) { # directory
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree->{$stem}) { } 1 1 2
104 1 spew_tree( "$top/$stem", $tree->{ $stem } );
105 }
106 else { # file
107 write_file( "$top/$stem", { binmode => ':raw' }, $tree->{ $stem } )
108 1 ▶ if defined $tree->{ $stem }; # avoid an undef warning
Branch: 1 branch
BranchTrueFalseTotal
if defined $tree->{$stem} 1 0 1
109 }
110 }
111 2 return 1;
112 }
113  
114 2 1;
115 __END__
↑ Summary