Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: use lib $lib_dir not working

by tinita (Parson)
on Apr 27, 2011 at 10:34 UTC ( #901521=note: print w/replies, xml ) Need Help??

in reply to use lib $lib_dir not working

use FindBin instead

Replies are listed 'Best First'.
Re^2: use lib $lib_dir not working
by Tanktalus (Canon) on Apr 27, 2011 at 21:04 UTC

    This is the solution I used to go with. However, I happened upon one of tye's rants in the CB a number of years ago, where he pointed out that FindBin is broken, and that you're better with File::Spec->rel2abs($0).

    I actually have the following in one of my scripts:

    use File::Spec; use File::Basename qw(dirname); use Cwd qw(abs_path); use lib File::Spec->catdir(dirname(dirname(abs_path($0))),'lib');
    Not necessarily the best way to do it, but close enough, I figure. And probably faster than FindBin

      that FindBin is broken

      well. the objections against FindBin I read so far always have to do with using FindBin after you changed the directory. If I had a script somewhere in my repository that does a chdir in a BEGIN block and if such a weird thing is really necessary, I can place FindBin even before that BEGIN block and everything's fine. If the script uses a module that does a chdir in a BEGIN block then I probably have other problems anyway.

      So if you know what you're doing IMHO FindBin is handy and safe enough. Do you always use $[ instead of 0 just because a script could set it somewhere? ;-)

      A reply falls below the community's threshold of quality. You may see it by logging in.
      Now I think I understood the problem. It is about the PATH searching feature which is only invoked if the script is called via "perl". (Documented is a workaround to call "perl ./", though. I guess I never had problems with this because I usually call scripts with the full path or at least bin/ anyway.)

      I was informed by Graham Barr that the PATH searching was done only because of a broken SunOS shell many years ago. I asked him because I suspected there must have been a reason for this.

      Moritz and I discussed and came to the conclusion that there is no other case where this "feature" would actually be reliable and useful. So to keep the nice short usage of FindBin and to make it work correctly in all cases (except doing chdir() before, since it is actually impossible to obtain the correct path then) I submitted a report to perlbug:

      update: link corrected, thanks

        You linked to the wrong bug. 89698 appears to be the bug you submitted (original link).

        And it appears that neither you nor Graham Barr actually understand what I find funny about searching PATH. It doesn't actually do anything useful (even in the case of a SunOS shell that could use relative paths). (For it to be useful, Perl itself would have to have a strange quirk, one that I'm pretty sure Perl never had.)

        - tye        

      use Path::Class; use lib dir( $0 , qw'.. .. lib')->absolute; use lib dir( file( $0 )->dir->parent, 'lib')->absolute;
Re^2: use lib $lib_dir not working
by sundialsvc4 (Abbot) on Apr 27, 2011 at 13:04 UTC

    This is an excellent suggestion.   FindBin will tell you where the script is actually executing.   It gives you a string, and you can go from there.

    It is a good idea to specify, one way or the other, fully qualified path names.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://901521]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2021-10-27 14:19 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (93 votes). Check out past polls.