Just a quick thing I found usefull when idiot proofing a script.

Basically I have a script and some very specific modules I want to use with it. And I want to enable the user to run it from anywhere easily, with relative or absolute paths. Which requires mucking with @INC (via use lib in this case). Yeah it's mind-numbingly easy, but useful to know. It also should be portable across operating systems I believe.

I came across FindBin while searchng for any previously posted nodes on this topic (after having already written the code), which accomplishes pretty much the same thing. But it also does a bunch of other stuff that brings it a bit over 3 lines, so thbbt. :)
#Note that this has to come before you include # the modules that are located in the script directory use File::Spec; use File::Basename; use lib &dirname(File::Spec->rel2abs($0));

Replies are listed 'Best First'.
•Re: Adding script location to include path
by merlyn (Sage) on Jul 10, 2003 at 21:58 UTC
      I was referring to FindBin itself, which uses the same libs as the snippet (File::Spec, and File::Basename), and brings in Cwd, Carp, and Config as well. It's not much more (and most likely already loaded by other things anyway) but uneeded in this particular case. :)

      Whichever's easier for you. Either way works fine (at least I think it does.. haven't extensively tested every scenario), and comes in handy.
        But your code also in turn pulls in those libs! Let's experiment:
        $ cat <<END >in-yours use File::Spec; use File::Basename; use lib &dirname(File::Spec->rel2abs($0)); print "$_\n" for sort keys %INC; END $ perl in-yours >out-yours $ cat <<END >in-mine use FindBin qw($Bin); use lib $Bin; print "$_\n" for sort keys %INC; END $ perl in-mine >out-mine $ comm out-yours out-mine Carp.pm Config.pm Cwd.pm Exporter.pm Exporter/Heavy.pm File/Basename.pm File/Spec.pm File/Spec/Unix.pm FindBin.pm XSLoader.pm attributes.pm base.pm lib.pm re.pm strict.pm vars.pm warnings.pm warnings/register.pm $
        So, the only differences are FindBin itself and Exporter::Heavy. Your attempt to avoid Cwd, Carp, and Config are not avoided in the slightest.

        So please, don't try to reinvent the wheel until you know what that wheel is doing.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.