Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Do you know where your variables are?
 
PerlMonks  

Where the perl scripts live...

by broquaint (Abbot)
on Sep 07, 2001 at 16:41 UTC ( [id://110936]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 John M. Dlugosz (Monsignor) on Sep 07, 2001 at 16:47 UTC
    print $0;
Re: Where the perl scripts live...
by suaveant (Parson) on Sep 07, 2001 at 16: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...
by LD2 (Curate) on Sep 07, 2001 at 17:00 UTC
Re: Where the perl scripts live (Russ - 'use lib' hack)
by Russ (Deacon) on Sep 07, 2001 at 17: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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://110936]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.