blib/lib/File/FindRoot.pm

filter: Statementstmt 100% 66 / 66 Branchbran 90.9% 20 / 22 Conditioncond 80.0% 12 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 92.3% 12 / 13 Podpod 100% 1 / 1 totaltotal 90.4% 113 / 125 CC 19 SCAR 24.9
CC
19
Cov
95%
dir_contains
19.1
__ANON__
1.0
BEGIN
1.0
CRAP 12.1
↑ Summary
1 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package File::FindRoot;
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = '0.004';
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 File::FindRoot - Find the directory that's the root for a project
21  
22 =head1 SYNOPSIS
23  
24 use File::FindRoot;
25  
26 Start looking in the current directory and in each ancestor directory
27 until you find one that contains the relative path. Return that directory:
28  
29 my $dir = File::FindRoot->dir_contains( $rel_path );
30 unless( defined $dir ) { ... }
31  
32 Or matches a pattern:
33  
34 my $dir = File::FindRoot->dir_contains( qr/$patten/ );
35  
36 Start in a different directory:
37  
38 my $dir = File::FindRoot->dir_contains( $file, { start_at => $path } );
39  
40 Limit the number of ancestors checked:
41  
42 my $dir = File::FindRoot->dir_contains( $file, { limit => $n } );
43  
44 =head1 DESCRIPTION
45  
46 Lately I've done a number of things where a program deep in a project had to
47 find its project config file or library directory.
48  
49 =over 4
50  
51 =item File::FindRoot->dir_contains( REL_PATH [, OPTIONS] )
52  
53 Returns the directory that contains C<REL_PATH>, and the empty list otherwise.
54  
55 =over 4
56  
57 =item * callback - (default: check that path exists) a subroutine that returns true
58 if the current directory is the one you want based on whatever you decide.
59  
60 =item * debug - (default: 0) if true, output progress information. If you do not
61 specify a value, it uses the defined value of the C<FILE_FINDROOT_DEBUG> environment
62 variable, or finally, 0.
63  
64 =item * debug_fh - (default: STDERR) the output filehandle for debugging info
65  
66 =item * limit - (default: inf) the maximum number of ancestors to inspect.
67  
68 =item * start_at - (default: current working directory) the directory in which
69 to start looking. If the string is not a directory, such as a filename, it uses
70 the directory name of the path. Any value is turned into an absolute path.
71  
72 =back
73  
74 The C<callback> argument takes a code reference with three positional parameters:
75 the current candidate directory, the passed C<REL_PATH> argument, and the C<OPTIONS>
76 hash:
77  
78 my $coderef = sub ( $candidate_dir, $target, $options ) { ... };
79 File::FindRoot->dir_contains( '.git', { callback => $coderef });
80  
81 If you don't specify a C<callback> argument, if uses one that catfiles
82 the directory and target and returns the value of C<-e> on the result:
83  
84 my $coderef = sub ($candidate_dir, $target, $options) {
85 -e File::Spec->catfile($candidate_dir, $target)
86 };
87  
88 =cut
89  
90 26 ▶sub dir_contains ($class, $target, $options = {}) {
Subroutine dir_contains: called CC=19 SCAR=29.5
Pod documented
91 26 ▶ unless( ref $options eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $options eq ref {}) 1 25 26
92 1 Carp::carp "dir_contains: options argument must be a hash reference";
93 1 return;
94 }
95  
96 25 ▶ $options->{'debug'} //= $ENV{'FILE_FINDROOT_DEBUG'} // 0;
Condition100%4/4
$options->{"debug"} //= $ENV{"FILE_FINDROOT_DEBUG"} // 0
l!l
619
$ENV{"FILE_FINDROOT_DEBUG"} // 0
l!l
217
MC/DC100%1/1
$options->{"debug"} //= $ENV{"FILE_FINDROOT_DEBUG"} // 0
$options->{"debug"}
Truth table100%2/2
A
A= $options->{"debug"}
Aresult
11
00
97 25 ▶ $options->{'debug_fh'} //= *STDERR;
Condition66%2/3
$options->{"debug_fh"} //= *STDERR
l!l&&r!l&&!r
9160
MC/DC0%0/2
$options->{"debug_fh"} //= *STDERR
$options->{"debug_fh"} *STDERR
Truth table66%2/3
A //= B
A= $options->{"debug_fh"}B= *STDERR
ABresult
1X1
011
000
98  
99 25 ▶ my $debug = ! $options->{'debug'} ? sub {} : sub ($message) { say { $options->{'debug_fh'} } "dir_contains: $message" };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
!$options->{'debug'} ? : 21 4 25
100  
101 25 ▶ $options->{'callback'} //= sub ($candidate_dir, $target, $options) { -e File::Spec->catfile($candidate_dir, $target) };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$options->{"callback"} //= sub ($candidate_dir, $target, $options) { -e "File::Spec"->catfile($candidate_dir, $target); }
l!l&&r!l&&!r
1240
MC/DC0%0/2
$options->{"callback"} //= sub ($candidate_dir, $target, $options) { -e "File::Spec"->catfile($candidate_dir, $target); }
$options->{"callback"} sub ($candidate_dir, $target, $options) { -e "File::Spec"->catfile($candidate_dir, $target); }
Truth table66%2/3
A //= B
A= $options->{"callback"}B= sub ($candidate_dir, $target, $options) { -e "File::Spec"->catfile($candidate_dir, $target); }
ABresult
1X1
011
000
102 25 ▶ unless( ref $options->{'callback'} eq ref sub {} ) {
Subroutine __ANON__: not called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $options->{"callback"} eq ref sub { } ) 1 24 25
103 1 Carp::carp "callback value is not a subroutine reference";
104 1 return;
105 }
106  
107 24 ▶ $options->{'limit'} //= (9**9**9);
Condition100%2/2
$options->{"limit"} //= "Inf"
l!l
123
MC/DC100%1/1
$options->{"limit"} //= "Inf"
$options->{"limit"}
Truth table100%2/2
A
A= $options->{"limit"}
Aresult
11
00
108 24 ▶ $options->{'start_at'} //= ( $^O eq 'MSWin32' ? Cwd::getdcwd() : Cwd::getcwd() );
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 16 16
Condition66%2/3
$options->{"start_at"} //= $^O eq "MSWin32" ? Cwd::getdcwd() : Cwd::getcwd()
l!l&&r!l&&!r
8160
MC/DC0%0/2
$options->{"start_at"} //= $^O eq "MSWin32" ? Cwd::getdcwd() : Cwd::getcwd()
$options->{"start_at"} $^O eq "MSWin32" ? Cwd::getdcwd() : Cwd::getcwd()
Truth table66%2/3
A //= B
A= $options->{"start_at"}B= $^O eq "MSWin32" ? Cwd::getdcwd() : Cwd::getcwd()
ABresult
1X1
011
000
109 24 my $original = $options->{'start_at'};
110  
111 24 $debug->( "before preprocessing, starting at <$original>" );
112 24 ▶ $options->{'start_at'} = File::Spec->rel2abs($options->{'start_at'}) unless File::Spec->file_name_is_absolute($options->{'start_at'});
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($options->{"start_at"}) 5 19 24
113 24 $options->{'start_at'} = Cwd::realpath($options->{'start_at'});
114 24 ▶ unless( length $options->{'start_at'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $options->{"start_at"}) 2 22 24
115 2 Carp::carp "Dir <$original> does not exist";
116 2 return;
117 }
118  
119 22 ▶ $options->{'start_at'} = File::Basename::dirname($options->{'start_at'}) if ! -d $options->{'start_at'};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $options->{"start_at"} 3 19 22
120 22 $debug->( "after preprocessing, starting at <$options->{'start_at'}>" );
121  
122 22 ▶ if( $options->{'limit'} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"limit"} < 0) 1 21 22
123 1 Carp::carp "Initial limit <$options->{'limit'}> was less than zero";
124 1 return;
125 }
126  
127 21 my $rounds = 0;
128 21 my $candidate_dir = $options->{'start_at'};
129 21 ▶ while( $rounds <= $options->{'limit'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rounds <= $options->{"limit"}) 32 0 32
130 32 $debug->( "round <$rounds> - looking in <$candidate_dir> for <$target>" );
131 32 ▶ if( $options->{'callback'}->( $candidate_dir, $target, $options ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"callback"}->($candidate_dir, $target, $options)) 20 12 32
132 20 return $candidate_dir;
133 }
134 12 my $ancestor = File::Basename::dirname($candidate_dir);
135 12 $debug->("ancestor is <$ancestor>");
136 12 ▶ last if $candidate_dir eq $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $candidate_dir eq $ancestor 1 11 12
137 11 $candidate_dir = $ancestor;
138 11 $rounds++;
139 }
140 1 $debug->( "stopped at <$candidate_dir> before finding <$target>" );
141  
142 1 return;
143 }
144  
145 =back
146  
147 =head1 TO DO
148  
149  
150 =head1 SEE ALSO
151  
152 =over 4
153  
154 =item * L<File::Find::>
155  
156 =back
157  
158 =head1 SOURCE AVAILABILITY
159  
160 This source is in Github:
161  
162 http://github.com/briandfoy/file-findroot
163  
164 =head1 AUTHOR
165  
166 brian d foy, C<< <briandfoy@pobox.com> >>
167  
168 =head1 COPYRIGHT AND LICENSE
169  
170 Copyright © 2026-2026, brian d foy, All Rights Reserved.
171  
172 You may redistribute this under the terms of the Artistic License 2.0.
173  
174 =cut
175  
176 3 1;
↑ Summary