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

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";
  • Comment on Re: how to get the current folder name where the program is running?
  • Download Code

Replies are listed 'Best First'.
Re: Re: how to get the current folder name where the program is running?
by exussum0 (Vicar) on Dec 13, 2003 at 13:01 UTC
    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

        There's always procfs.. but that's very OS dependent.

        Play that funky music white boy..
Re: Re: how to get the current folder name where the program is running?
by Anonymous Monk on Dec 13, 2003 at 20:43 UTC
    Did you peek inside FindBin? You're basically starting to reinvent FindBin :)