Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl and current directory

Reply
Thread Tools

Perl and current directory

 
 
Jørn Dahl-Stamnes
Guest
Posts: n/a
 
      12-09-2006
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.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-09-2006
Jørn Dahl-Stamnes wrote:
> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Jørn Dahl-Stamnes
Guest
Posts: n/a
 
      12-09-2006
Gunnar Hjalmarsson wrote:

> Jørn Dahl-Stamnes wrote:
>> 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 =~ /(.+)\//;


$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.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-09-2006
Jørn Dahl-Stamnes wrote:
> Gunnar Hjalmarsson wrote:
>>Jørn Dahl-Stamnes wrote:
>>>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 =~ /(.+)\//;

>
> $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);

>>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.


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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
kens
Guest
Posts: n/a
 
      12-09-2006

Jørn Dahl-Stamnes wrote:
> 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.
>
> --
> Jørn Dahl-Stamnes
> http://www.dahl-stamnes.net/dahls/


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

 
Reply With Quote
 
Jørn Dahl-Stamnes
Guest
Posts: n/a
 
      12-09-2006
Gunnar Hjalmarsson wrote:

> Then, maybe, that is what you should use:
>
> print qx(pwd);


THANKS...

>>>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.

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


Yes... I agree. The machine is using FC4.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
 
Reply With Quote
 
Jørn Dahl-Stamnes
Guest
Posts: n/a
 
      12-09-2006
kens wrote:

>
> Jørn Dahl-Stamnes wrote:
>> 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.
>>
>> --
>> Jørn Dahl-Stamnes
>> http://www.dahl-stamnes.net/dahls/

>
> You do not mention which OS you are using.


Sorry. That was a *big* mistake of me. I'm using Fedora Core 4.

--
Jørn Dahl-Stamnes
http://www.dahl-stamnes.net/dahls/
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      12-09-2006

"Jørn Dahl-Stamnes" <> wrote in message
news:...
> Gunnar Hjalmarsson wrote:
>
>> Jørn Dahl-Stamnes wrote:
>> 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.


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



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      12-09-2006

"Jørn Dahl-Stamnes" <> wrote in message
news:...
> Gunnar Hjalmarsson wrote:
>
>> Then, maybe, that is what you should use:
>>
>> print qx(pwd);

>
> 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.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      12-09-2006
Jørn Dahl-Stamnes wrote:

>> You do not mention which OS you are using.

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printing out the current URL for current ASP page in Perl Jack Perl Misc 3 12-22-2008 02:35 PM
Apache vs IIS current directory fro Perl script andipfaff Perl Misc 6 05-13-2008 06:49 AM
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
current directory name in perl dennishancy@eaton.com Perl Misc 10 08-15-2006 03:01 PM
how to use Perl to rename the filenames and directory names under current and subdirectories recursively? lucy Perl Misc 6 09-03-2004 06:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57