Home

Search this site:
 

Horshack’s
very
own site

 

This is Bavaria, Germany!
You will find links to Perl,
bootdisks for system administration,
a lot of Linux,
Windows administration
and some personal belongings.

Finding current directory, corresponding directory to programm, the path to your inifile etc.

Getting a knowledge about the location of your perl-file, the ini-file in the same directory where your .pl-file lies and things like that:

This is the program:

use strict;
use warnings;
use Cwd;
use File::Basename;
use File::Spec;
# The directory where you are just now
printf "cwd = %s\n", Cwd::cwd();
# always absolute path of program
# -> /tmp
printf "directory of program itself = %s\n",
    File::Basename::dirname(Cwd::abs_path($0));
# relative or absolute path of program, depends on 
# how you call it: 
# ./x.pl -> .
# cd /a/b; perl /tmp/x.pl -> /tmp
printf "directory of program itself = %s\n", dirname($0);
# program-name including the absolute path
# -> /tmp/x.pl
printf "abs_path of program itself = %s\n", Cwd::abs_path($0);
# our program-name without path
printf "basename of program itself = %s\n", File::Basename::basename($0);
# a corresponding ini-file without path
# x.pl -> x.ini
my @suffixes = ('.pl');
printf "inifile for program itself without path = %s\n",
    File::Basename::basename($0, @suffixes) . '.ini';
printf "inifile for program itself with path = %s\n",
    File::Spec->catfile(
        File::Basename::dirname(Cwd::abs_path($0)),
        File::Basename::basename($0, @suffixes) . '.ini'
    );

The output is:

$ cd /tmp
$ perl x.pl
cwd = /tmp
directory of program itself = /tmp
directory of program itself = .
abs_path of program itself = /tmp/x.pl
basename of program itself = x.pl
inifile for program itself without path = x.ini
inifile for program itself with path = /tmp/x.ini
Impressum