FindBin(3perl)
π FindBin β the manual page, but make it a group chat π
okay bestie, so this is FindBin, a little module from the Perl Programmers Reference Guide section 3, and it is genuinely one of the most useful little gremlins in the whole core distribution. sit down, grab a strawberry π, let's go.
β¨ NAME β¨
FindBin β literally just: locate the directory of the original Perl script. that's it. that's the whole job. and it ATE. π
πΈ SYNOPSIS πΈ
use FindBin;
use lib "$FindBin::Bin/../lib";
use FindBin qw($Bin);
use lib "$Bin/../lib";
see how clean that is?? iconic. no cap.
π¦ DESCRIPTION π¦
so what it does is: it finds the full path to the script's bin directory, so you can use paths that are relative to that bin directory. that's the entire vibe. π
which means you can set up a cute little directory tree for your software with a <root>/bin directory and a <root>/lib directory, and then the example above just works β you can use modules from the lib directory without ever needing to know where the software tree actually got installed on the machine. main character energy fr fr. β¨
and ifperlgets invoked with the-eoption, or your Perl script is being read fromSTDIN? thenFindBinsets both$Binand$RealBinto the current directory. not the script's directory. the current one. noted. π
π EXPORTABLE VARIABLES π
here's the lineup, bestie. memorize it, it's like three seconds of your life:
$Binor$Dirβ the path to the bin directory from where the script was invoked π«Ά$Scriptβ the basename of the script from whichperlwas invoked$RealBinor$RealDirβ$Bin, but with all the links resolved π$RealScriptβ$Script, but with all the links resolved
and if you want all of them at once (greedy, love that for you), there's the ALL tag:
use FindBin ':ALL';
periodt. π
π KNOWN ISSUES π
okay. this is the part where we get real. π«
if there are two modules using FindBin from different directories under the same interpreter, this won't work. and the reason is so petty: FindBin uses a BEGIN block, so it only gets executed once, and only the first caller gets it right. everyone after that is just out here living a lie. π
this is a genuine problem under mod_perl and other persistent Perl environments β and in those environments you shouldn't use this module at all. which also means you should avoid using FindBin in modules that you plan to put on CPAN. i'm so serious. π
the fix: call the again function to make sure FindBin actually works:
use FindBin;
FindBin::again(); # or FindBin->again;
and in former versions of FindBin there was no again function (chaotic era, honestly), so the workaround was to force the BEGIN block to be executed again:
delete $INC{'FindBin.pm'};
require FindBin;
look at her. vintage. π
π«Ά AUTHORS π«Ά
FindBin is supported as part of the core perl distribution. if you find a bug, please submit bug reports at:
<https://github.com/Perl/perl5/issues>
and shoutout to the actual legends:
Graham Barr <gbarr@pobox.com> Nick Ing-Simmons <nik@tiuk.ti.com>
we stan. π
β COPYRIGHT β
Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. slay, legally speaking. π
okay that's the whole page, bestie, you're officially literate in FindBin now. go forth and find your bin. π¦πΈπ
and for the record: this was perl v5.44.0, dated 2026-10-17, from FindBin(3perl). iykyk β¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms