Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to get the current folder name where the program is running?

by hweefarn (Acolyte)
on Dec 13, 2003 at 04:04 UTC ( [id://314470] : perlquestion . print w/replies, xml ) Need Help??

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

hello everyone :) today is a beautiful day :)

let's say i have write a program called sample.pl and i put it in /home/aaa/. so, the location of this program is /home/aaa/sample.pl

so, when i am running this program, i need to get the folder name "aaa" to be used as a variable inside this program. how can i do so?

my real situation is like this:
when there is new mails come in, my procmail will automatically run this program "sample.pl" at /home/aaa/sample.pl. then, in this program, i need to retrieve some data from the database using the user id, which is "aaa". that's why i need to get the folder name "aaa". how can i do so?

thank you very much :)

regards,
hweefarn :)
  • Comment on how to get the current folder name where the program is running?

Replies are listed 'Best First'.
Re: how to get the current folder name where the program is running?
by Roger (Parson) on Dec 13, 2003 at 04:32 UTC
    You could use the CORE module File::Spec::Functions to retreive the name of the parent directory of the perl script, in a portable way that works under both Windows and Linux/Unix. It works even when your current working directory is not the same as the directory of the perl script.

    #!/usr/local/bin/perl -w use strict; use File::Spec::Functions qw/ splitdir rel2abs /; my $parent_dir = (splitdir(rel2abs($0)))[-2]; print "$parent_dir\n";
      To ammend your reply, use $0 (which the parent did), not cwd() (which many recommend), since $0 has the full name of the running script and cwd is where the script was run from. If you aren't in that directory where it was run, cwd fails miserably.

      From perlvar....

      $0 Contains the name of the program being executed. On some oper- ating systems assigning to `$0' modifies the argument area that the ps program sees. This is more useful as a way of indicat- ing the current program state than it is for hiding the program you're running. (Mnemonic: same as sh and ksh.)

      Update: Ug.. as tachyon showed, it doesn't always work. Try /procfs, it should tell you what file you are currently running.. though windows and osx doesn't have that.


      Play that funky music white boy..

        So you tested this right?

        [root@smart sporty]# pwd /root/sporty [root@smart sporty]# cat uhuh.pl #!/usr/bin/perl print "$0 does not know\n", `pwd`; [root@smart sporty]# ./uhuh.pl ./uhuh.pl does not know /root/sporty [root@smart sporty]# perl uhuh.pl uhuh.pl does not know /root/sporty [root@smart sporty]#

        If you aren't in that directory where it was run, cwd fails miserably.

        And if (you) don't call the script with /full/path/to/script.pl then $0 has little to offer....

        cheers

        tachyon

      Did you peek inside FindBin? You're basically starting to reinvent FindBin :)
Re: how to get the current folder name where the program is running?
by Anonymous Monk on Dec 13, 2003 at 04:22 UTC
    use FindBin;
Re: how to get the current folder name where the program is running?
by etcshadow (Priest) on Dec 13, 2003 at 04:32 UTC
    use Cwd; my ($bottomdir) = cwd() =~ m|([^/]*)$|;

    ------------
    :Wq
    Not an editor command: Wq
Re: how to get the current folder name where the program is running?
by tachyon (Chancellor) on Dec 13, 2003 at 04:26 UTC
      Wrong. This gets you the directory you are currently in, your current working dir. If I do /Users/sporty/t.pl, and I'm in /Applications, I'd get /Applications, not /Users/sporty/t.pl

      Play that funky music white boy..
Re: how to get the current folder name where the program is running?
by traxlog (Scribe) on Dec 13, 2003 at 11:43 UTC
    $ENV{'SCRIPT_FILENAME'}
    and some regex wizardry always works for me!

    -2 votes
    May I ask why?
    Maybe I'll learn something too.
    Update: For those of you arriving at my node via a search please note it is NOT the right answer, see rest of thread

    Thanks for the enlightenment Joost and Anon! Indeed something learned!
      -2 votes May I ask why? Maybe I'll learn something too.
      Probably because your answer is wrong (hweefarn doesn't seem to be dealing with a CGI).