Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to find out the directory in which the perl script is located? 2

Status
Not open for further replies.

plasmatwin

Programmer
Nov 1, 2005
31
GB
ok, so if I have a perl script in a directory (eg "C:\perlscripts\")and I go to that directory and run it then "./config.conf" in the script will refer to the file "config.conf" in the same directory as the script ("C:\perlscripts\config.conf"), but if I am in a different directory (eg "C:\") and I type in "C:\perlscripts\script.pl" to execute the script then ./config.conf will refer to "C:\config.conf"

How do I find out what directory the perl script is in so that I can avoid this problem?
 
Use absolute references in your script. If the file config.conf is in C:/perlscripts/ refer to it as C:/perlscripts/config.conf

HTH
--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
The location of the script itself if held in the special $0 variable.
 
If you put the directory of the script in the system variable PATH, you will be able to execute the script from any directory without use the full path.

Windows: Go to control panel->System->advanced->environment variables

Unix:
set path=(/mydirectory/ $path /usr/sbin) it could be copied in your .cshrc

Cheers

dmazzini
GSM System and Telecomm Consultant

 
For unix:


my $script = $0;
$script =~ s/^[\.\/\\]+//;
$ABSOLUTE_PATH = `pwd`;
chomp($ABSOLUTE_PATH);
$ABSOLUTE_PATH .= "/" . $script;
if ($ABSOLUTE_PATH =~ /(.*)\/([\w]+.pl)/) {
$ABSOLUTE_PATH = $1;
$script = $2;
}


dmazzini
GSM System and Telecomm Consultant

 
ok, so in windows I get a path like "C:\perlscripts\script.pl" returned from the $0 variable but in unix I will get something like "/home/graham/perlscripts/script.pl" (assuming that I keep my scripts there) returned from that variable. This makes things harder for me if I want to make my script cross-platform... should I use a regular expression to translate all my backslashes in the variable to be front slashes and then trim off the name of the script so that I am left with a path to the script? I can do the translation with no problems I think:
Code:
my $scriptdir = $0;
$scriptdir =~ tr/\\/\//;
That works with no problems and won't make a difference if it is run on a unix machine, now what do I do to trim off the script name from the end?
 
Have a look at File::Spec

There's platform-agnostic functions in there to help
 
perl doesn't care about forward or back slashes. And windows doesn't either as far as I know. So there is really very little to worry about with the direction of the slashes in path statements.
 
I just tend to use front slashes... it's just me, the main reason is I want to strip the script name off the end ("C:/perlscripts/script.pl" becomes "C:/perlscripts/") and so I would need all the slashes to be pointing in one direction so that the next statement works on any OS. So how would I strip the name off the end?
 
you can use the file::basename module or substr() or a regexp, or even split(). file::basename is easy enough to use.

forward slashes is easier in perl since the backslash is used for characters (\s,\t,\n) and character classes (\w,\d,\W) and escaping.
 
ok, I've got something that works... I'm sure it can be tidied up... can anyone help me out on this?
Code:
 #!perl -w
my $scriptdir = $0;
$scriptdir =~ tr/\\/\//;
($scriptdir) = ($scriptdir =~ /^((?:.*[:\\\/])?)(.*)/s);
print "$scriptdir\n";
 
never mind, got the final piece of code sorted...
Code:
#!perl -w
my $scriptdir = $0;
$scriptdir =~ tr/\\/\//;
$scriptdir =~ (/^((?:.*[:\\\/])?)(.*)/s)[0];
print "$scriptdir\n";
that solves my problem...
 
I'd go with Kevin's suggestion of File::Basename;
Code:
use File::Basename;
my $path = dirname($0);
print "$path\n";
If you want a trailing / or \, you'll need to add one.
 
I'll second (third?) the File::Basename suggestion. That's the generally accepted "right way" to do this. If you use File::Basename and File::Spec to do your path manipulation, you never have to worry about what kind of slashes your current platform uses.
 
If you're really in the mood to use a regex, here's a slightly cleaner one for you:
Code:
(my $path) = $0 =~ m!^(.+[\\/])!;
print "$path\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top