broquaint has asked for the wisdom of the Perl Monks concerning the following question:

I want to find out the current directory of the perl script I'm in e.g
use Cwd; print cwd()."\n";
However, the above code tells you where the script is being executed from so
# /usr/local/src/perl/> perl test.pl /usr/local/src/perl
works fine and dandy but this isn't so good
# /home/foobar/> perl /usr/local/src/perl/test.pl /home/foobar
as I still want to get /usr/local/src/perl as that's where it lives.
Is there a nice perl-y way of doing this? Or am I going to have to resort to the dark arts of 'nasty hacking'?

broquaint

Replies are listed 'Best First'.
Re: Where the perl scripts live...
by suaveant (Parson) on Sep 07, 2001 at 20:57 UTC
    There is also
    use FindBin; $FindBin::Bin
    which gives you just the directory the script is in

                    - Ant
                    - Some of my best work - Fish Dinner

      Excellent!
      That's exactly want I wanted.
      'Many thanks' x $alot;

      broquaint

Re: Where the perl scripts live (Russ - 'use lib' hack)
by Russ (Deacon) on Sep 07, 2001 at 21:01 UTC
    Noting that I do not necessarily recommend this procedure, yet use it on occasion (in an environment where I implicitly trust the user - me):
    # Ugly trick to let us use our modules while calling the script from e +lsewhere use lib substr($0, 0, rindex($0, '/'));
    I now have access to the script's directory and can use modules located there, even though I was in another directory when I executed the script.

    $Pretty--
    $Effective++

    Russ
    Brainbench 'Most Valuable Professional' for Perl

Re: Where the perl scripts live...
by LD2 (Curate) on Sep 07, 2001 at 21:00 UTC
Re: Where the perl scripts live...
by John M. Dlugosz (Monsignor) on Sep 07, 2001 at 20:47 UTC
    print $0;