lib::abs(3)
Okay bestie, welcome to the lib::abs(3) glow-up β itβs a User Contributed Perl Documentation page, and itβs here to make your relative paths absolutely iconic. π
β¨
π NAME π
lib::abs β the "lib" that makes relative path absolute to caller. π
π¦ SYNOPSIS π¦
Simple use, like "use lib ...":
use lib::abs qw(./mylibs1 ../mylibs2);
use lib::abs 'mylibs';
# if your path may not exists and it is ok, then:
use lib::abs -soft => qw(./mylibs1 ../mylibs2);
Extended syntax (glob) π
use lib::abs 'modules/*/lib';
Thereβs also a helper function you can use from lib::abs (check example/ex4):
use lib::abs;
# ...
my $path = lib::abs::path('../path/relative/to/me'); # returns absolute path
πΈ DESCRIPTION πΈ
The main reason this library exists is to transform relative paths into absolute paths at the BEGIN stage, then push the transformed paths into @INC. The basis for a relative path is not the current working directory β itβs the location of the file where the statement lives, aka the caller file. π
When you use regular lib, relative paths stay relative to the current working directory instead. π
# For ex:
# script: /opt/scripts/my.pl
use lib::abs '../lib';
# We run `/opt/scripts/my.pl` having cwd /home/mons
# The @INC will contain '/opt/lib';
# We run `./my.pl` having cwd /opt
# The @INC will contain '/opt/lib';
# We run `../my.pl` having cwd /opt/lib
# The @INC will contain '/opt/lib';
This module is also a lifesaver when youβre writing tests and want to load strictly the module from ../lib, respecting the test file. π§
# t/00-test.t
use lib::abs '../lib';
Itβs also useful when youβre running under mod_perl, using something like Apache::StatINC, and your application may change its working directory. Because in the case of chdir, StatINC fails to reload the module if @INC contains relative paths. π
π RATIONALE π
Q: We already have FindBin and lib, why we need this module?
A: There are several reasons:
FindBincould find the path incorrectly undermod_perl.FindBinworks relatively to the executed binary instead of relatively to the caller.- Perl is a linguistic language, and
use lib::abs "..."is semantically more clear and looks more beautiful thanuse FindBin; use lib "$FindBin::Bin/../lib";. FindBinwill work incorrectly if itβs called not from the executed binary (seehttp://github.com/Mons/lib-abs-vs-findbincomparison for details).
π BUGS π
None known. (Iconic, honestly.) β¨
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2007-2020 by Mons Anderson.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
π AUTHOR π
Mons Anderson, "<mons@cpan.org>"
π«Ά CONTRIBUTORS π«Ά
Oleg Kostyuk, "<cub@cpan.org>"
π Baked with perl v5.42.2 on 2026-02-15 β lib::abs(3), signing off! π
π¦ where this page comes from
- alpine 3.24: perl-lib-abs-doc 0.95-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms