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