The Fixer said over 5 years ago permalink Comment? (0)
Tagged: perl

Perl use lib

I recently had occasion to break up an overly-large Perl script into a couple of modules, but I didn’t particularly want to install the modules anyplace. For one thing, I’m still working on them, but I also want to use a current (working!) version in production. Keeping the modules next to the script seemed like a nice straightforward thing to do, and it even worked, so long as the current working directory was the same as the directory the script and its modules were located in, but didn’t work so well from a different directory.

The solution is the FindBin module, which has some heuristics to find where the currently running script is actually located, and make that directory available to the rest of the program as a nice neat variable. Here’s what bit me; the syntax to actually make use of this looks like this:

use FindBin;
use lib "$FindBin::Bin";

What I failed to notice is that the second line is NOT a fill-in-the-name-of-your-library-here thing, it’s a literal thing. It is supposed to be followed by code that actually loads the libraries you want, only now it will look in the same directory as where the script is loaded from as well.

use FindBin;
use lib "$FindBin::Bin";

use MyLibrary;
use MyOtherLibrary;

“use lib” is not a command to load a library, it’s a “pragma”, which alters the way Perl works. Yet another bit of the whale guts in Perl, I suppose. Since I don’t write in Perl all that often, I suppose I really ought to dig out my copy of the camel book and keep it in a more accessible place so I don’t embarrass myself like this every time I try.

Comments

simple_captcha.jpg
Are you a Human? Type the code above.