File Coverage

blib/lib/FindBin/Parents.pm
Criterion Covered Total %
statement 34 34 100.0
branch 4 6 66.6
condition 4 6 66.6
subroutine 8 8 100.0
pod 0 2 0.0
total 50 56 89.2


line stmt bran cond sub pod time code
1             ########################################################################
2             # housekeeping
3             ########################################################################
4             package FindBin::Parents v0.1.0;
5 11     11   349941 use v5.40;
  11         41  
6 11     11   71 use parent qw( Exporter );
  11         23  
  11         101  
7              
8 11     11   940 use Carp qw( croak );
  11         56  
  11         826  
9 11     11   68 use List::Util qw( reduce );
  11         32  
  11         846  
10 11     11   1617 use Storable qw( dclone );
  11         20203  
  11         1130  
11              
12             use File::Spec::Functions
13             qw
14 11         4796 (
15             splitpath
16             splitdir
17             catdir
18             catpath
19             rel2abs
20             canonpath
21 11     11   636 );
  11         1190  
22              
23             ########################################################################
24             # package variables and sanity checks
25             ########################################################################
26              
27             our @EXPORT_OK
28             = qw
29             (
30             dir_paths
31             clear_parent_cache
32             );
33              
34             my %path2dirz = ();
35              
36             ########################################################################
37             # utility subs
38             ########################################################################
39              
40             ########################################################################
41             # exported
42             ########################################################################
43              
44             # avoid issues with repeated use on different paths.
45              
46 1     1 0 2072 sub clear_parent_cache()
  1         2  
47             {
48 1         3 %path2dirz = ();
49             }
50              
51 18         40 sub dir_paths( $path, $assume_dir = 1 )
  18         45  
52 18     18 0 2742 {
  18         27  
53 18 50       63 $path
54             or croak 'Bogus dir_paths: false path argument.';
55              
56             my $dirz
57             = $path2dirz{ $path . $; . $assume_dir }
58             ||= do
59 18   66     112 {
60             # treat non-existant paths as dir's, mainly for testing.
61              
62 10   66     426 my $is_dir = $assume_dir || -d $path;
63              
64 10         84 my ( $vol, $dir ) = splitpath $path, $is_dir;
65              
66             # ditch the starting directory.
67              
68 10         179 my @dirz = splitdir rel2abs canonpath $dir;
69              
70             # fix for File::Spec::VMS missing the leading empty
71             # string on a split. this can be removed once File::Spec
72             # is fixed -- which appears to be never.
73              
74 10 50       329 my $tmp
75             = $dirz[0]
76             ? ''
77             : shift @dirz
78             ;
79              
80             [
81             reverse
82             map
83             {
84 55         285 catpath $vol => $_, ''
85             }
86             map
87             {
88 10         27 $tmp = catdir $tmp, $_
  55         216  
89             }
90             ( '' => @dirz )
91             ]
92             };
93              
94             wantarray
95 18 100       183 ? @$dirz
96             : [ @$dirz ]
97             }
98              
99             1
100             __END__