in reply to use lib qw(relative path)

Sounds like you need FindBin

use FindBin qw($Bin); use lib "$Bin/lib";
--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: use lib qw(relative path)
by ccn (Vicar) on Sep 25, 2004 at 19:09 UTC

    Hmmm.... It's solution but it is not appropriate for some hostings. I got fatal errors if I use FindBin qw($Bin $Script)

    opendir(/pub/home/****/cgi-bin/**/*****//../../../..): Permission deni +ed at /usr/libdata/perl/5.00503/FindBin.pm line 162 opendir(/pub/home/****/cgi-bin/**/*****//../../../..): Permission deni +ed at /usr/libdata/perl/5.00503/FindBin.pm line 163 Can't locate *****.pm in @INC (@INC contains: /lib /usr/local/lib/perl +5/site_perl/5.005/i386-freebsd /usr/local/lib/perl5/site_perl/5.005 . /usr/libdata/perl/5.00503/mach +/usr/libdata/perl/5.00503) at /pub/home/****/cgi-bin/**/*****/**.pl line 10.

    Now I write:

    use vars qw($Bin $Script); BEGIN { my $pos = rindex $0, '/'; $pos = rindex $0, "\\" if $pos == -1; if ($pos != -1) { $Bin = substr($0, 0, $pos); $Script = substr($0, $pos+1); } else { $Bin = '.'; $Script = $0; } } use lib "$Bin/lib";

    Can it be done better?

      I don't know whether your algorithm itself can be written in another way to make it better do FindBin's job, but you can make your algorithm more portable.

      If you want it to be portable you probably want to use File::Basename. Also, &File::Basename::dirname takes care of the special case of the argument being just a file with not directory in it, and returning ".". To continue being portable, use catfile instead of joining then strings with a slash.

      use File::Basename qw/ dirname basename /; use File::Spec::Functions qw/ catfile /; BEGIN { $Bin = dirname($0); $Script = basename($0); $Lib = catfile($Bin, 'lib'); } use lib $Lib;

      ihb

      Read argumentation in its context!

        This one is fine. But I prefer less keystrokes to make copy-paste more easy.

        use vars qw($Bin $Script); BEGIN { ($Bin, $Script) = split /([^\/\\]+)$/, $0 } use lib $Bin . 'lib';

        It works for Win, Unix/Linux. I don't care about Mac

        UPDATE: It works on Mac too. see Re: Paths in Perl

      I think it would be far more useful finding out why FindBin isn't working and spending time fixing that, rather than wasting time reimplementing it.

      We might be able to give you more help if there weren't so many ***s in your error messages :)

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        I beleive that I can't fix it. I have no contact with admin of that hosting. Please see without '***':

        cs7793@v25:~#perl -e 'use FindBin' opendir(./..): Permission denied at /usr/libdata/perl/5.00503/FindBin. +pm line 97 cs7793@v25:~#perldoc -m FindBin|grep VERSION $VERSION = $VERSION = "1.42"; cars7793@v25:~#perldoc -m FindBin|perl -ne 'print if $. == 97' $Bin = $RealBin = getcwd(); cs7793@v25:~#perl -e 'use Cwd; print Cwd::getcwd' opendir(./..): Permission denied at -e line 1 cs7793@v25:~#perldoc -m Cwd |grep VERSION $VERSION = '2.01'; cs7793@v25:~#

        Your move! :)

Re^2: use lib qw(relative path)
by ccn (Vicar) on Sep 25, 2004 at 12:30 UTC

    Yes, it is the solution. Thanks

Re^2: use lib qw(relative path)
by Aristotle (Chancellor) on Sep 26, 2004 at 15:33 UTC