blib/lib/lib/relative.pm

filter: Statementstmt 100% 25 / 25 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 97.2% 35 / 36 CC 3 SCAR 5.1
CC
3
Cov
97%
import
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.7
↑ Summary
1 package lib::relative;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use lib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.002';
11  
12 3 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
13 3 my ($class, @paths) = @_;
14 3 my $file = (caller)[1];
15 3 ▶ my $dir = -e $file ? File::Basename::dirname(Cwd::abs_path $file) : Cwd::getcwd;
Branch: 1 branch
BranchTrueFalseTotal
-e $file ? : 3 0 3
16 3 ▶ lib->import(map { File::Spec->file_name_is_absolute($_) ? $_ : File::Spec->catdir($dir, $_) } @paths);
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($_) ? : 1 2 3
17 }
18  
19 1 1;
20  
21 =head1 NAME
22  
23 lib::relative - Add paths relative to the current file to @INC
24  
25 =head1 SYNOPSIS
26  
27 # Path is relative to this file, not current working directory
28 use lib::relative 'path/to/lib';
29 use lib::relative '../../lib';
30
31 # Add two lib paths, as in lib.pm
32 use lib::relative 'foo', 'bar';
33
34 # Absolute paths are passed through unchanged
35 use lib::relative 'foo/baz', '/path/to/lib';
36
37 # Equivalent code using core modules
38 use Cwd ();
39 use File::Basename ();
40 use File::Spec ();
41 use lib File::Spec->catdir(File::Basename::dirname(Cwd::abs_path __FILE__), 'path/to/lib');
42  
43 =head1 DESCRIPTION
44  
45 Adding a path to L<@INC|perlvar/"@INC"> to load modules from a local directory
46 may seem simple, but has a few common pitfalls to be aware of. Directly adding
47 a relative path to C<@INC> means that any later code that changes the current
48 working directory will change where modules are loaded from. This applies to
49 the C<.> path that used to be in C<@INC> by default until perl 5.26.0, or a
50 relative path added in code like C<use lib 'path/to/lib'>, and may be a
51 vulnerability if such a location is not supposed to be writable. Additionally,
52 the commonly used L<FindBin> module relies on interpreter state and the path to
53 the original script invoked by the perl interpreter, sometimes requiring
54 workarounds in uncommon cases like generated or embedded code. This module
55 proposes a more straightforward method: take a path relative to the
56 L<current file|perldata/"Special Literals">, absolutize it, and add it to
57 C<@INC>.
58  
59 If this module is already available to be loaded, it can be used as with
60 L<lib>.pm, passing relative paths, which will be absolutized relative to the
61 current file then passed on to L<lib>. Multiple arguments will be separately
62 absolutized, and absolute paths will be passed on unchanged.
63  
64 For cases where this module cannot be loaded beforehand, the last section of
65 the L</"SYNOPSIS"> can be copy-pasted into a file to perform the same task.
66  
67 =head1 CAVEATS
68  
69 Due to C<__FILE__> possibly being a path relative to the current working
70 directory, be sure to use C<lib::relative> or the equivalent code from
71 L</"SYNOPSIS"> as early as possible in the file. If a C<chdir> occurs before
72 this code, it will add the incorrect directory path.
73  
74 All file paths are expected to be in a format appropriate to the current
75 operating system, e.g. C<..\\foo\\bar> on Windows. L<File::Spec/"catdir"> can
76 be used to form directory paths portably.
77  
78 =head1 BUGS
79  
80 Report any issues on the public bugtracker.
81  
82 =head1 AUTHOR
83  
84 Dan Book <dbook@cpan.org>
85  
86 =head1 COPYRIGHT AND LICENSE
87  
88 This software is Copyright (c) 2017 by Dan Book.
89  
90 This is free software, licensed under:
91  
92 The Artistic License 2.0 (GPL Compatible)
93  
94 =head1 SEE ALSO
95  
96 L<lib>, L<FindBin>, L<Dir::Self>
↑ Summary