Perl and current directory

  • Thread starter Jørn Dahl-Stamnes
  • Start date
J

Jørn Dahl-Stamnes

How can a perl script find the absolute path to itself?

I have tried to do a 'print $0;', but $0 only tells me the command used to
invoke the script.

What I need to find is the absolute path to the directory where the perl
script is located.
 
G

Gunnar Hjalmarsson

Jørn Dahl-Stamnes said:
How can a perl script find the absolute path to itself?

I have tried to do a 'print $0;', but $0 only tells me the command used to
invoke the script.

What I need to find is the absolute path to the directory where the perl
script is located.

print $0 =~ /(.+)\//;

Or more portable:

use FindBin '$Bin';
print $Bin;
 
J

Jørn Dahl-Stamnes

Gunnar said:
print $0 =~ /(.+)\//;

$0 does not contain the absolute path. If I do a:

cd ~/some_dir
../some_perl_script.pl

$0 will contain "./some_perl_script.pl".

What I need is to get exactly the same as the pwd command prints out.
Or more portable:

use FindBin '$Bin';
print $Bin;

Seems like I need to download some kind of module. FindBin is not installed
at my system.
 
G

Gunnar Hjalmarsson

Jørn Dahl-Stamnes said:
$0 does not contain the absolute path. If I do a:

cd ~/some_dir
./some_perl_script.pl

$0 will contain "./some_perl_script.pl".

What I need is to get exactly the same as the pwd command prints out.

Then, maybe, that is what you should use:

print qx(pwd);
Seems like I need to download some kind of module. FindBin is not installed
at my system.

Strange, since it's part of the standard Perl distribution.
 
K

kens

Jørn Dahl-Stamnes said:
How can a perl script find the absolute path to itself?

I have tried to do a 'print $0;', but $0 only tells me the command used to
invoke the script.

What I need to find is the absolute path to the directory where the perl
script is located.

You do not mention which OS you are using.
If you are on a Win32 system, you can use the Win32::GetFullPathName
instead of FindBin. Of course it would be less portable.

use strict;
use warnings;
use Win32;
use File::Basename;
my $tmp = dirname( "$0" );
my $runDir = Win32::GetFullPathName($tmp);

HTH, Ken
 
T

Tintin

Jørn Dahl-Stamnes said:
Seems like I need to download some kind of module. FindBin is not
installed
at my system.

If you have FC4, then your Perl installation is broken if FindBin is
missing. Show us the exact error message you received.
 
T

Tintin

Jørn Dahl-Stamnes said:
THANKS... :)

The above will give you incorrect results. You originally stated "How can a
perl script find the absolute path to itself?"

Running pwd will just return your current working directory, which may or
may not be the same path as where the Perl script lives.

The correct answer is still to use the FindBin module, however, we'll need
to work out why your Perl installation is broken first.
 
J

Joe Smith

Jørn Dahl-Stamnes said:
Sorry. That was a *big* mistake of me. I'm using Fedora Core 4.

mathras> cat /etc/redhat-release
Fedora Core release 4 (Stentz)
mathras> locate FindBin
/usr/lib/perl5/5.8.6/FindBin.pm
 
J

Jørn Dahl-Stamnes

Tintin said:
If you have FC4, then your Perl installation is broken if FindBin is
missing. Show us the exact error message you received.

Strange... it works now. Perhaps I had some kind of typo-error that I did
not see.

Thanks to all.
 
J

Jørn Dahl-Stamnes

Tintin said:
The above will give you incorrect results. You originally stated "How can
a perl script find the absolute path to itself?"

Running pwd will just return your current working directory, which may or
may not be the same path as where the Perl script lives.

I found out this morning that it would not work as expected. It did work
when I did a './script.pl', but it did not work when the perl script was
invoked through cron.
The correct answer is still to use the FindBin module, however, we'll need
to work out why your Perl installation is broken first.

Seems to work after all. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top