Skip to content

Instantly share code, notes, and snippets.

@leingang
Last active October 26, 2017 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leingang/c49cbf37100ecbb6e2f58ca7761ef712 to your computer and use it in GitHub Desktop.
Save leingang/c49cbf37100ecbb6e2f58ca7761ef712 to your computer and use it in GitHub Desktop.
How to change @inc to point to a relative directory
use File::Basename qw(dirname);
use FindBin qw($Bin);
use lib dirname($Bin) . '/lib';
@leingang
Copy link
Author

leingang commented Oct 26, 2017

See How to create a Perl Module for code reuse? by Gabor Szabo, in particular the first comment by Nigel Peck.

Say you have a perl project with executables in bin/ and modules in lib/. How do you get the executables to locate the modules?

  • Line 1 imports the dirname function from File::Basename, which does what you think it does.
  • Line 2 uses the FindBin module to locate the "bin" directory of the called program. It doesn't seem that that directory needs to be named bin; it's just the name of the including directory. This directory name is stored in $Bin.
  • Line 3 takes the directory that includes $Bin, attaches /lib, and appends this to @INC. In other words, the sister directory to $Bin named lib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment