Wednesday, December 1, 2010

Day 1: add relative paths to @INC with rlib

Welcome to day 1 of the Perl-Uwe Advent calendar. We start with a very small CPAN module, which is one of my favorites. It's quite old (its last release was 1998), but it still does its job. :)

When my Perl projects are larger than a single script file, I usually create folders like this:
bin/
lib/
t/

You know what goes into what folder. And especially in the scripts which use your own modules you want to include something like this:
use lib '../lib';

This works fine when you call this script from your project root directory, but nowhere else. :(

rlib to the rescue!

use rlib '../lib';

This is equivalent to:
use FindBin;
use lib "$FindBin::Bin/../lib";

Like "use lib" you can add more than one directory to @INC. If you specify no path at all, rlib uses "../lib" and "lib" as defaults. So, in our example we could have written "use rlib;" alone.

Links:

1 comment:

  1. See also lib::abs for a little more modern version of the same basic functionality.

    ReplyDelete

Note: Only a member of this blog may post a comment.