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

Is any simple way to get the current direcoty name? e.g, if the current path is '/user/john', how to assign a variable with this string ? thank you!

edited: Mon Jul 15 19:15:32 2002 by jeffa - title typo correction

  • Comment on Is any simple way to get the current directory name?

Replies are listed 'Best First'.
Re: Is any simple way to get the current direcoty name?
by broquaint (Abbot) on Jul 15, 2002 at 17:44 UTC
    Check out the Cwd module which comes with the core perl distrubution.
    HTH

    _________
    broquaint

Re: Is any simple way to get the current direcoty name?
by chromatic (Archbishop) on Jul 15, 2002 at 17:45 UTC

    The Cwd module comes standard with modern Perls and is portable and fairly smart.

Re: Is any simple way to get the current direcoty name?
by joealba (Hermit) on Jul 15, 2002 at 17:41 UTC
    Note: Bad answer about using $ENV{PWD} removed (instead of using my whiteout).

    Use Cwd.
      The problem with $ENV{PWD} is that it doesn't stay "linked." i.e.
      print $ENV{PWD},"\n"; chdir("..") print $ENV{PWD},"\n";
      will print the same thing twice, which is probably not what you want. (If you use Cwd and ask it to override chdir, it will automatically keep $ENV{PWD} in sync with the actual current working directory, but by default it is not kept in sync).

      --JAS
Re: Is any simple way to get the current directory name?
by Corion (Patriarch) on Jul 15, 2002 at 21:47 UTC

    Another module I'm fond of is the File::chdir module, which allows easy access to the name of the current directory as well as easy manipulation of the current directory (it allows treating the path to the current directory as an array, together with pushing, popping and localizing values in it).

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      There's two ways you could approach this..
      The win32 way.
      #!/usr/bin/perl -w use strict; use Win32; my $dir = Win32::GetCwd; print "$dir";
      or the other way..
      #!/usr/bin/perl -w use strict; use Cwd; my $dir = cwd; print "$dir";