| 1 |
NAME |
| 2 |
|
| 3 |
lib::relative - Add paths relative to the current file to @INC |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
# Path is relative to this file, not current working directory |
| 8 |
use lib::relative 'path/to/lib'; |
| 9 |
use lib::relative '../../lib'; |
| 10 |
|
| 11 |
# Add two lib paths, as in lib.pm |
| 12 |
use lib::relative 'foo', 'bar'; |
| 13 |
|
| 14 |
# Absolute paths are passed through unchanged |
| 15 |
use lib::relative 'foo/baz', '/path/to/lib'; |
| 16 |
|
| 17 |
# Equivalent code using core modules |
| 18 |
use Cwd (); |
| 19 |
use File::Basename (); |
| 20 |
use File::Spec (); |
| 21 |
use lib File::Spec->catdir(File::Basename::dirname(Cwd::abs_path __FILE__), 'path/to/lib'); |
| 22 |
|
| 23 |
DESCRIPTION |
| 24 |
|
| 25 |
Adding a path to @INC to load modules from a local directory may seem |
| 26 |
simple, but has a few common pitfalls to be aware of. Directly adding a |
| 27 |
relative path to @INC means that any later code that changes the |
| 28 |
current working directory will change where modules are loaded from. |
| 29 |
This applies to the . path that used to be in @INC by default until |
| 30 |
perl 5.26.0, or a relative path added in code like use lib |
| 31 |
'path/to/lib', and may be a vulnerability if such a location is not |
| 32 |
supposed to be writable. Additionally, the commonly used FindBin module |
| 33 |
relies on interpreter state and the path to the original script invoked |
| 34 |
by the perl interpreter, sometimes requiring workarounds in uncommon |
| 35 |
cases like generated or embedded code. This module proposes a more |
| 36 |
straightforward method: take a path relative to the current file, |
| 37 |
absolutize it, and add it to @INC. |
| 38 |
|
| 39 |
If this module is already available to be loaded, it can be used as |
| 40 |
with lib.pm, passing relative paths, which will be absolutized relative |
| 41 |
to the current file then passed on to lib. Multiple arguments will be |
| 42 |
separately absolutized, and absolute paths will be passed on unchanged. |
| 43 |
|
| 44 |
For cases where this module cannot be loaded beforehand, the last |
| 45 |
section of the "SYNOPSIS" can be copy-pasted into a file to perform the |
| 46 |
same task. |
| 47 |
|
| 48 |
CAVEATS |
| 49 |
|
| 50 |
Due to __FILE__ possibly being a path relative to the current working |
| 51 |
directory, be sure to use lib::relative or the equivalent code from |
| 52 |
"SYNOPSIS" as early as possible in the file. If a chdir occurs before |
| 53 |
this code, it will add the incorrect directory path. |
| 54 |
|
| 55 |
All file paths are expected to be in a format appropriate to the |
| 56 |
current operating system, e.g. ..\\foo\\bar on Windows. "catdir" in |
| 57 |
File::Spec can be used to form directory paths portably. |
| 58 |
|
| 59 |
BUGS |
| 60 |
|
| 61 |
Report any issues on the public bugtracker. |
| 62 |
|
| 63 |
AUTHOR |
| 64 |
|
| 65 |
Dan Book <[email protected]> |
| 66 |
|
| 67 |
COPYRIGHT AND LICENSE |
| 68 |
|
| 69 |
This software is Copyright (c) 2017 by Dan Book. |
| 70 |
|
| 71 |
This is free software, licensed under: |
| 72 |
|
| 73 |
The Artistic License 2.0 (GPL Compatible) |
| 74 |
|
| 75 |
SEE ALSO |
| 76 |
|
| 77 |
lib, FindBin, Dir::Self |
| 78 |
|