File Coverage

blib/lib/Path/This.pm
Criterion Covered Total %
statement 80 83 96.3
branch 21 26 80.7
condition n/a
subroutine 19 21 90.4
pod 2 2 100.0
total 122 132 92.4


line stmt bran cond sub pod time code
1             package Path::This;
2              
3 1     1   65969 use strict;
  1         10  
  1         28  
4 1     1   6 use warnings;
  1         1  
  1         33  
5 1     1   6 use Carp ();
  1         2  
  1         13  
6 1     1   4 use Cwd ();
  1         1  
  1         21  
7 1     1   6 use File::Basename ();
  1         1  
  1         12  
8 1     1   461 use Sub::Util ();
  1         299  
  1         176  
9              
10             our $VERSION = '0.003';
11              
12 3     3 1 2558 sub THISFILE () { Cwd::abs_path((caller)[1]) }
13             sub THISDIR () {
14 3     3 1 12 my $file = (caller)[1];
15 3 100       115 return -e $file ? File::Basename::dirname(Cwd::abs_path $file) : Cwd::getcwd;
16             }
17              
18             sub import {
19 6     6   878 my $class = shift;
20 6         20 my ($package, $file) = caller;
21              
22 6         12 my ($abs_file, $abs_dir);
23 6         13 foreach my $item (@_) {
24 12 100       70 if ($item =~ m/\A([&\$])?THISFILE\z/) {
    50          
25 6         14 my $symbol = $1;
26 6 100       13 unless (defined $abs_file) {
27 3         42 $abs_file = Cwd::abs_path $file;
28             }
29 6 100       20 if (!$symbol) {
    100          
    50          
30 2         3 my $const_file = $abs_file;
31 1     1   7 no strict 'refs';
  1         2  
  1         39  
32 1     1   6 no warnings 'redefine';
  1         2  
  1         92  
33 2         3 *{"${package}::THISFILE"} = \&{Sub::Util::set_subname
  2         1419  
34 2     0   18 "${package}::THISFILE", sub () { $const_file }};
  0         0  
35             } elsif ($symbol eq '&') {
36 1     1   6 no strict 'refs';
  1         2  
  1         54  
37 1     1   6 no warnings 'redefine';
  1         2  
  1         87  
38 2         4 *{"${package}::THISFILE"} = \&THISFILE;
  2         219  
39             } elsif ($symbol eq '$') {
40 1     1   6 no strict 'refs';
  1         2  
  1         126  
41 2         4 *{"${package}::THISFILE"} = \$abs_file;
  2         57  
42             }
43             } elsif ($item =~ m/\A([&\$])?THISDIR\z/) {
44 6         16 my $symbol = $1;
45 6 50       16 unless (defined $abs_dir) {
46 6 100       337 $abs_dir = defined $abs_file ? File::Basename::dirname($abs_file)
    50          
47             : -e $file ? File::Basename::dirname($abs_file = Cwd::abs_path $file)
48             : Cwd::getcwd;
49             }
50 6 100       29 if (!$symbol) {
    100          
    50          
51 2         4 my $const_dir = $abs_dir;
52 1     1   7 no strict 'refs';
  1         1  
  1         26  
53 1     1   5 no warnings 'redefine';
  1         1  
  1         107  
54 2         3 *{"${package}::THISDIR"} = \&{Sub::Util::set_subname
  2         16  
55 2     0   36 "${package}::THISDIR", sub () { $const_dir }};
  0         0  
56             } elsif ($symbol eq '&') {
57 1     1   7 no strict 'refs';
  1         2  
  1         43  
58 1     1   6 no warnings 'redefine';
  1         1  
  1         61  
59 2         5 *{"${package}::THISDIR"} = \&THISDIR;
  2         11  
60             } elsif ($symbol eq '$') {
61 1     1   13 no strict 'refs';
  1         2  
  1         128  
62 2         4 *{"${package}::THISDIR"} = \$abs_dir;
  2         11  
63             }
64             } else {
65 0           Carp::croak qq{"$item" is not exported by the $class module};
66             }
67             }
68             }
69              
70             1;
71              
72             =head1 NAME
73              
74             Path::This - Path to this source file or directory
75              
76             =head1 SYNOPSIS
77              
78             use Path::This '$THISFILE';
79             print "This file is $THISFILE\n";
80              
81             use Path::This '$THISDIR';
82             use lib "$THISDIR/../lib";
83              
84             =head1 DESCRIPTION
85              
86             Exports package variables by request that represent the current source file or
87             directory containing that file. Dynamic or constant sub versions can also be
88             requested. Paths will be absolute with symlinks resolved.
89              
90             Note that the package variable or constant sub will be exported to the current
91             package globally. If the same package will be used in multiple files, use the
92             dynamic sub export so the file path will be calculated when the sub is called.
93              
94             =head1 EXPORTS
95              
96             =head2 $THISFILE
97              
98             =head2 &THISFILE
99              
100             =head2 THISFILE
101              
102             print "$THISFILE\n";
103             my $file = THISFILE;
104              
105             Absolute path to the current source file. Behavior is undefined when called
106             without a source file (e.g. from the command line or STDIN). C<$THISFILE> will
107             export a package variable, C<&THISFILE> will export a dynamic subroutine, and
108             C will export an inlinable constant.
109              
110             =head2 $THISDIR
111              
112             =head2 &THISDIR
113              
114             =head2 THISDIR
115              
116             print "$THISDIR\n";
117             my $dir = THISDIR;
118              
119             Absolute path to the directory containing the current source file, or the
120             current working directory when called without a source file (e.g. from the
121             command line or STDIN). C<$THISDIR> will export a package variable, C<&THISDIR>
122             will export a dynamic subroutine, and C will export an inlinable
123             constant.
124              
125             =head1 BUGS
126              
127             Report any issues on the public bugtracker.
128              
129             =head1 AUTHOR
130              
131             Dan Book
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is Copyright (c) 2019 by Dan Book.
136              
137             This is free software, licensed under:
138              
139             The Artistic License 2.0 (GPL Compatible)
140              
141             =head1 SEE ALSO
142              
143             L, L, L