Perl Newbies

  1. Perl
  2. Modules
  3. here

FindBin - Get the path of the directory where the script exists

FindBin module gets the path of the directory where the script exists.

use FindBin;

The path of the directory where the script exists is assigned to $FindBin::Bin package variable.

# Get the path of the directory where the script exists
my $script_dir = $FindBin::Bin;

FindBin is a little strange module. but it is a standard way to get the path of the directory where the script exists.

Set the path to load modules

FindBin module is offten used together with lib module.

You can set the path to load modules to the path of the directory where the script exists.

# Set the path to load modules to "lib" in the directory where the script exists.
use FindBin;
use lib "$FindBin::Bin/lib";

Script base name

You can get the base name of the script using $FindBin::Script.

use FindBin;
$FindBin::Script
Menu