Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (files)

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I get the full path for a file?
by Fastolfe (Vicar) on Jun 22, 2002 at 23:44 UTC
    If you have a filename, then you probably already have some "relative" path to that filename, even if we're making the assumption that you're working from the current directory.

    In this case, File::Spec will do the job for you with its rel2abs function:

    $abs_path = File::Spec->rel2abs($filename);
    If you have a file but have no idea where it's located, you might want to use File::Find.
Re: how do I get fullpath for a file
by turnstep (Parson) on Apr 14, 2000 at 00:48 UTC

    Add this to the top of your code:

    use Cwd;

    Then you can just:

    $thisdir = cwd();

    This gets the directory the "safest" way. If you are definitely on unix, however, you can simply say:

    $thisdir = `pwd`;
    which is usually what Cwd does on unix systems. It has a few other options, such as fastcwd() and getcwd() but they are not used much. You can grab the name of the script itself by looking in $0 or even __FILE__.

    If you are doing it through the web, check out the environment variable 'SCRIPT_FILENAME'

    $pwd = $ENV{'SCRIPT_FILENAME'};