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

Hi, to all!
I need to recieve the location directory where I'm running the program.   I tried to use this :

my $dir1=$0;

but it gives me the directory with the file , for example c:\perl\exmp.pl and I need only c:\perl\.

Thank you.

janitored by ybiC: Retitle from less-than-descriptive "the corrent location" for better site searching, and minor format tweaks for legibility.

  • Comment on How to obtain current working directory

Replies are listed 'Best First'.
Re: How to obtain current working directory
by davido (Cardinal) on Mar 23, 2004 at 09:28 UTC
    That's what Cwd.pm is for.

    use Cwd; my $dir = getcwd; print "$dir\n";

    Cwd.pm is the preferable method. However, if you really need to use $0 instead of Cwd, you could use File::Basename like this:

    use File::Basename; my $dir = dirname( $0 ); print "$dir\n";

    Update: As davis has reminded us, the current working directory is not necessarily the same thing as the directory where the script is located. If you want the former, use the Cwd approach. If you want the latter, use the dirname( $0 ) approach.


    Dave

Re: How to obtain current working directory
by davis (Vicar) on Mar 23, 2004 at 09:39 UTC

    Your question is ambiguous. Do you want "Where I am now", or "Where the script located"? If you want the first option, then davido's solution above will do, whereas if you want the second option, I'd probably use File::Basename's dirname function on $0, something like:

    my $dir = dirname($0);


    davis
    It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: How to obtain current working directory
by Aragorn (Curate) on Mar 23, 2004 at 09:38 UTC
    You mean you need only the directory where the script resides? Then File::Basename (standard module) will do the trick:
    use File::Basename; my $script_dir = dirname($0);
    Arjen
Re: How to obtain current working directory
by pelagic (Priest) on Mar 23, 2004 at 09:49 UTC
    Nelly,
    I strongly recommend to use File::Spec for this task ... and here is a example:
    #!/usr/bin/perl use strict; use File::Spec; print "File::Spec::VERSION ", $File::Spec::VERSION, "\n"; # looking for our scripts directory my $fullprogram = $0; my (undef,undef,$program) = File::Spec->splitpath( $fullprogram ); print $fullprogram, "\n"; print $program, "\n"; # looking for current position my $cur_dir = File::Spec->curdir(); print $cur_dir, "\n"; my $abs_path = File::Spec->rel2abs( $cur_dir ) ; print $abs_path, "\n";
    I assume You want your current path and NOT you currently executing program.
    pelagic

    -------------------------------------
    I can resist anything but temptation.
Re: How to obtain current working directory
by eserte (Deacon) on Mar 23, 2004 at 12:13 UTC
    The only real solution (platform independent etc.) is the FindBin module: use FindBin qw($Bin); # $Bin is your script's directory
      File::Spec is also platform independent and btw Nelly wanted the current position not the script's directory.
      pelagic

      -------------------------------------
      I can resist anything but temptation.