File Coverage

blib/lib/File/ShareDir/Tiny.pm
Criterion Covered Total %
statement 42 42 100.0
branch 13 16 81.2
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 69 72 95.8


line stmt bran cond sub pod time code
1             package File::ShareDir::Tiny;
2             $File::ShareDir::Tiny::VERSION = '0.001';
3 3     3   490988 use strict;
  3         6  
  3         115  
4 3     3   14 use warnings;
  3         6  
  3         226  
5              
6 3     3   19 use Exporter 5.57 'import';
  3         72  
  3         371  
7             our @EXPORT_OK = qw/dist_dir module_dir dist_file module_file/;
8             our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
9              
10 3     3   21 use Carp 'croak';
  3         11  
  3         217  
11 3     3   1558 use File::Spec::Functions qw/catfile catdir/;
  3         2677  
  3         1763  
12              
13             sub _search_inc_path {
14 9     9   59 my $path = catdir(@_);
15              
16 9         47 for my $candidate (@INC) {
17 37 50       94 next if ref $candidate;
18 37         167 my $dir = catdir($candidate, $path);
19 37 100       2286 return $dir if -d $dir;
20             }
21              
22 4         16 return undef;
23             }
24              
25             sub dist_dir {
26 4     4 1 1228 my $dist = shift;
27              
28 4 50       19 croak 'No dist given' if not length $dist;
29 4         12 my $dir = _search_inc_path('auto', 'share', 'dist', $dist);
30              
31 4 100       300 croak("Failed to find share dir for dist '$dist'") if not defined $dir;
32 2         5 return $dir;
33             }
34              
35             sub module_dir {
36 7     7 1 398213 my $module = shift;
37              
38 7 100       340 croak 'No module given' if not length $module;
39 5         33 (my $module_dir = $module) =~ s/::/-/g;
40 5         21 my $dir = _search_inc_path('auto', 'share', 'module', $module_dir);
41              
42 5 100       348 croak("Failed to find share dir for module '$module'") if not defined $dir;
43 3         9 return $dir;
44             }
45              
46             sub dist_file {
47 2     2 1 1923 my ($dist, @file) = @_;
48 2         10 my $dir = dist_dir($dist);
49 1         6 my $path = catfile($dir, @file);
50 1 50       33 -e $path or croak(sprintf 'File \'%s\' does not exist in dist dir of %s', catfile(@file), $dist);
51 1         6 return $path;
52             }
53              
54             sub module_file {
55 2     2 1 1955 my ($module, @file) = @_;
56 2         8 my $dir = module_dir($module);
57 2         15 my $path = catfile($dir, @file);
58 2 100       258 -e $path or croak(sprintf 'File \'%s\' does not exist in module dir of %s', catfile(@file), $module);
59 1         5 return $path;
60             }
61              
62             1;
63              
64             #ABSTRACT: Locate per-dist and per-module shared files
65              
66             __END__