lib::relative(3)
β¨ lib::relative(3) β the perl one, bestie β¨
okay so first things first, this is a page from the User Contributed Perl Documentation section of the manual (that's the perl manual, section 3), and it's about a module called lib::relative. buckle up π
πΈ NAME πΈ
lib::relative β adds paths to @INC that are relative to the current file π
π SYNOPSIS π
like, here's the whole vibe in code form:
# Path is relative to this file, not current working directory
use lib::relative 'path/to/lib';
use lib::relative '../../lib';
# Add two lib paths, as in lib.pm
use lib::relative 'foo', 'bar';
# Absolute paths are passed through unchanged
use lib::relative 'foo/baz', '/path/to/lib';
# Equivalent code using core modules
use Cwd ();
use File::Basename ();
use File::Spec ();
use lib File::Spec->catdir(File::Basename::dirname(Cwd::abs_path __FILE__), 'path/to/lib');
π DESCRIPTION π
okay so adding a path to @INC so you can load modules out of a local directory sounds like the easiest thing ever, right? wrong, bestie π there are a few iconic pitfalls and you Need to know about them.
if you just plop a relative path straight into @INC, then any code that runs later and changes the current working directory will legit change where your modules get loaded from. yikes. and this applies to both:
- the
"."path that used to be in@INCby default until perl5.26.0, and - any relative path you add in code like
use lib 'path/to/lib'
and ngl, that can be a whole vulnerability if that location isn't supposed to be writable π
also, the super commonly used FindBin module? it relies on interpreter state and on the path to the original script that the perl interpreter was invoked with, which sometimes means you need workarounds in uncommon cases β like generated code, or embedded code. not it π
so this module proposes a much more straightforward method: take a path relative to the current file, absolutize it, and add it to @INC. periodt β¨
if this module is already available to be loaded, you can use it exactly like lib.pm β pass it relative paths and they get absolutized relative to the current file, then passed on to lib. multiple arguments each get absolutized separately, and absolute paths are passed on unchanged. slay π«Ά
and for the cases where you can't load this module beforehand? the last chunk of the SYNOPSIS can literally just be copy-pasted into your file to do the exact same task. no cap.
π CAVEATS π
because __FILE__ might be a path that's relative to the current working directory, you gotta use lib::relative (or the equivalent code from SYNOPSIS) as early as possible in the file. seriously. if a chdir happens before that code runs, it will add the totally incorrect directory path. π¬
also, all file paths are expected to be in a format that's appropriate for the current operating system β e.g. ..\foo\bar on Windows. you can use catdir in File::Spec to form directory paths portably. π§
π¦ BUGS π¦
report any issues on the public bugtracker. that's it, that's the whole section π
π AUTHOR π
Dan Book <dbook@cpan.org>
π COPYRIGHT AND LICENSE π
this software is Copyright (c) 2017 by Dan Book.
it's free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
β SEE ALSO β
lib, FindBin, Dir::Self
and that's the whole page, bestie π«Ά this was perl v5.42.0, dated 2024-03-08, and it's the lib::relative(3) page β go forth and absolutize your paths πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-lib-relative 1.2.0-bp160.1.11 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms