Find::Lib appears to be exactly that.

use Find::Lib '../lib';

By the way, you have abs_path (aka realpath) and dirname backwards.

use Cwd qw( abs_path ); use File::Basename qw( dirname ); use lib dirname(abs_path($0)).'/../lib';

The order in which you use them will fail if you create a symlink to the script.

Update: Find::Lib touts its handling of symlinks, but it fails to handle a symlink to the script.

$ find ! -type d -exec ls -l {} + lrwxrwxrwx 1 eric users 8 Feb 22 17:04 ./a.pl -> bin/a.pl -rwx------ 1 eric users 53 Feb 22 17:04 ./bin/a.pl -rw------- 1 eric users 16 Feb 22 17:03 ./lib/Mod.pm $ cat bin/a.pl #!/usr/bin/env perl use Find::Lib '../lib'; use Mod; $ cat lib/Mod.pm package Mod; 1; $ bin/a.pl $ a.pl Can't locate Mod.pm in @INC (@INC contains: /home/eric/usr/perlbrew/pe +rls/perl-5.12.2/lib/site_perl/5.12.2/i686-linux /home/eric/usr/perlbr +ew/perls/perl-5.12.2/lib/site_perl/5.12.2 /home/eric/usr/perlbrew/per +ls/perl-5.12.2/lib/5.12.2/i686-linux /home/eric/usr/perlbrew/perls/pe +rl-5.12.2/lib/5.12.2 .) at ./a.pl line 3. BEGIN failed--compilation aborted at ./a.pl line 3.

In reply to Re: adding library directories relative to $0 by ikegami
in thread adding library directories relative to $0 by perl5ever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.