| 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
| |||||||||||||||||||||||||||||
| 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
$ENV{"FILE_FINDROOT_DEBUG"} // 0
MC/DC100%1/1
$options->{"debug"} //= $ENV{"FILE_FINDROOT_DEBUG"} // 0
$options->{"debug"}
Truth table100%2/2
A
A= $options->{"debug"}
| |||||||||||||||||||||||||||||
| 97 | 25 | ▶ | $options->{'debug_fh'} //= *STDERR; | ||||||||||||||||||||||||||
|
Condition66%2/3
$options->{"debug_fh"} //= *STDERR
MC/DC0%0/2
$options->{"debug_fh"} //= *STDERR
$options->{"debug_fh"}
*STDERR
Truth table66%2/3
A //= B
A= $options->{"debug_fh"}B= *STDERR
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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);
}
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);
}
| |||||||||||||||||||||||||||||
| 102 | 25 | ▶ | unless( ref $options->{'callback'} eq ref sub {} ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$options->{"limit"} //= "Inf"
$options->{"limit"}
Truth table100%2/2
A
A= $options->{"limit"}
| |||||||||||||||||||||||||||||
| 108 | 24 | ▶ | $options->{'start_at'} //= ( $^O eq 'MSWin32' ? Cwd::getdcwd() : Cwd::getcwd() ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options->{"start_at"} //= $^O eq "MSWin32" ? Cwd::getdcwd() : Cwd::getcwd()
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()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 113 | 24 | $options->{'start_at'} = Cwd::realpath($options->{'start_at'}); | |||||||||||||||||||||||||||
| 114 | 24 | ▶ | unless( length $options->{'start_at'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 22 | $debug->( "after preprocessing, starting at <$options->{'start_at'}>" ); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 22 | ▶ | if( $options->{'limit'} < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 32 | $debug->( "round <$rounds> - looking in <$candidate_dir> for <$target>" ); | |||||||||||||||||||||||||||
| 131 | 32 | ▶ | if( $options->{'callback'}->( $candidate_dir, $target, $options ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||