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

The title is a little vague :)

What I'm trying to do is figure out where a script is run from. I'm trying to do this from both a windows enviroment and unix...

Here is an example...

If I put the perl script in ~/bin and I type mailme.pl from another location. It should find a configuration file I've got in ~/bin.

Instead, it tries to find it in the current directory.

here is the relevant portion of my script...

use File::Basename; # Get configuration information my $baseLoc = dirname($0); print "$baseLoc\n"; my %configs; getConfig($baseLoc);

Replies are listed 'Best First'.
Re: Getting a directory location
by tommyw (Hermit) on Jul 02, 2002 at 14:55 UTC

    use File::Basename; my $baseLoc=($0=~/\//)?dirname($0): (grep { -x "$_/$0"} split ":", $ENV{"PATH"})[0];
    That is, if there's a / in the executable, all well and good: it's been told precisely where it is. Otherwise, check through all the elements of the PATH environment variable, until we find an executable with this name.

    Note that this will go horribly wrong if somebody invokes your script directly using perl, rather than letting the kernel decide on the interpreter, as a few seconds thought about what'll happen if the execution command was: perl ls

    Update:dmerphq has just pointed out in the CB that $0 is always set to the full path and filename, if the user has relied on the normal path searching. So you can get away with just using dirname($0)

    see Re: How do I get the full path to the script executing? for a longer explanation.

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

      This works really well for me, thanks. A little modification to it, and it doesn't even matter what platform it's on.

      With this I don't have to hardcode the config file, which is great 'cause I didn't want to create an install for it either.

      Now to testing, then I may even post the code :)

Re: Getting a directory location
by softworkz (Monk) on Jul 02, 2002 at 14:53 UTC
    Yes, vague indeed.. here's my 2 cents
    #!/usr/bin/perl -w use strict; use Win32; my $dir = Win32::GetCwd(); print "$dir\n"; # Or try this #my $baseLoc = "c:\\web"; #my $baseLoc = "c:\\wb\\configFile.pl"; #print "$baseLoc\n";
Re: Getting a directory location
by greenFox (Vicar) on Jul 03, 2002 at 03:01 UTC

    As softworkz and tommyw have said if $0 does not contain a path (ie invoked as ./script.pl) then pwd should be OK -provided you have not changed directories before you read it. However I would suggest that the best solution is to code the location of the config file. I usually end up with something like:

    # untested code... my $config_dir = "/path/to/script/config"; my $config_file = $config_dir . "/" . "config.file"; my %config = read_config($config_file); sub read_config { my $config = shift || die "Config file name not supplied \n"; open(CONFIG, $config) || die "Could not read from $config: $!\n"; # code to parse config file goes here close(CONFIG); return %config; # or @config or .... }

    Then it doesn't matter how your script is invoked, it will always know where to look for its config file.

    --
    my $chainsaw = 'Perl';