Q:

How to have conditional libraries with Perl?

I have homemade #Perl libraries  that I'm moving from machine to machine, and the paths are not the same from place to place. Specifically, on the old machines, they existed in /home/foo/lib/, while they're moving to /group/bar/apps/lib on the new machines, and I now want to have them in something like /home/me/dev/Tools/lib.
What we did was have multiple use lib lines. /home/foo isn't available on the new machine, and /group/bar isn't a directory on the old machine, so when it sees this --
use lib '/home/foo/lib/' ;
use lib '/group/bar/apps/lib' ;
use Tools::Foo ;

-- everything is fine.
The problem is, they link to each other, and I'd rather not have something in /home/me/dev/Tools/lib load a program from /group/bar/apps/lib, and when I move this stuff to production, I don't want to have anything pointing back to ~/me/dev. Preferrably, I would want to not have to modify the code when I move it into production, so that, when everything is deployed, diff /group/bar/apps/lib/Tools/Foo.pm /home/me/dev/Tools/lib/Tools/Foo.pm would be empty. 
So, how do I set things for multiple conditional library locations?

A:

Options:

Properly install the modules.
Place your modules relative to the script
use FindBin qw( $RealBin );
use lib "$RealBin/../lib";  # Or whatever.

Use environment variable PERL5LIB rather than use lib.
The statements could be placed in sitecustomize.pl (if support for sitecustomize.pl was enabled when perl was built).

A:

Using the following pragma
package lib_first_of;

use lib ();
use strict;
use warnings;

use Carp;

sub import {
  foreach my $path (@_) {
    if (-d $path) {
      lib->import($path);
      return 1;
    }
  }

  croak "$0: no suitable library path found";
}

1;

lets your main program have the form
#! /usr/bin/env perl

use strict;
use warnings;

use lib_first_of (
  "/home/foo/lib",
  "/group/bar/apps/lib",
);

use MyModule;

print "done.\n";

If neither path is present, the program fails with errors resembling
my-program: no suitable library path found at my-program line 7
BEGIN failed--compilation aborted at my-program line 9.

