Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I find program location?

28 views
Skip to first unread message

John Brock

unread,
Dec 20, 2001, 11:41:30 PM12/20/01
to
I want my Perl program to be able to find its absolute location
on both Windows and Unix, no matter how it is called. In particular,
on Windows the path must include the disk letter (e.g.,
E:\mystuff\progs\some_dir\my_prog). How do I do this?

On Unix I was able to write a program which could figure this out
from the $0 variable and the cwd() function (Cwd module). (Note I
need the current working directory to handle situations where the
program was called by relative path, e.g., ../some_dir/my_prog).

But on Windows I run into a problem: there are multiple current
working directories, one for each disk! Let's say I am somewhere
on the D: disk and I call my program like this: E:..\some_dir\my_prog.
In order to find the absolute path to my program I need to know
what directory I would be in if I changed disks by typing 'E:'.
Is there a way to find this out without actually changing disks
and calling the cwd() function?

Alternatively, is there a better and more standard way to do what
I am trying to do?
--
John Brock
jbr...@panix.com

Jürgen Exner

unread,
Dec 21, 2001, 12:06:15 AM12/21/01
to
"John Brock" <jbr...@panix.com> wrote in message
news:9vuehq$g5j$1...@panix3.panix.com...

> I want my Perl program to be able to find its absolute location
> on both Windows and Unix, no matter how it is called. In particular,
> on Windows the path must include the disk letter (e.g.,
> E:\mystuff\progs\some_dir\my_prog). How do I do this?

See the FindBin module "perldoc FindBin"

jue


Wiliam Stephens

unread,
Dec 21, 2001, 6:02:56 AM12/21/01
to John Brock
[ posted and mailed ]

John Brock wrote:
> [ snip ]


>
> Alternatively, is there a better and more standard way to do what
> I am trying to do?


Standard? We're talking Perl here. TMTOWTDI.


I'm using the following code, it works for me. Hope this helps!


if ($0=~m#^(.*)\\#){ $cgidir = "$1"; } # Win32/DOS
elsif ($0=~m#^(.*)/# ){ $cgidir = "$1"; } # Unix
else {`pwd` =~ /(.*)/; $cgidir = "$1"; } # Unix

You need the last else line as some unix servers don't put the full file
path into the $0 variable.

--
Wiliam Stephens <w...@stephens.org>>

John W. Krahn

unread,
Dec 21, 2001, 7:50:58 AM12/21/01
to
Wiliam Stephens wrote:
>
> [ posted and mailed ]
>
> John Brock wrote:
> > [ snip ]
> >
> > Alternatively, is there a better and more standard way to do what
> > I am trying to do?
>
> Standard? We're talking Perl here. TMTOWTDI.
>
> I'm using the following code, it works for me. Hope this helps!
>
> if ($0=~m#^(.*)\\#){ $cgidir = "$1"; } # Win32/DOS
> elsif ($0=~m#^(.*)/# ){ $cgidir = "$1"; } # Unix
> else {`pwd` =~ /(.*)/; $cgidir = "$1"; } # Unix
^^^^^^
What is this supposed to do, remove newlines? Have you not heard of
chomp()? Why the quotation marks around the $1 variable, it's already a
string?

John
--
use Perl;
program
fulfillment

Wiliam Stephens

unread,
Dec 21, 2001, 8:06:13 AM12/21/01
to
John W. Krahn wrote:

It works :-)

--
Wiliam Stephens <w...@stephens.org>

Bart Lateur

unread,
Dec 21, 2001, 8:08:53 AM12/21/01
to
John Brock wrote:

>I want my Perl program to be able to find its absolute location
>on both Windows and Unix, no matter how it is called. In particular,
>on Windows the path must include the disk letter (e.g.,
>E:\mystuff\progs\some_dir\my_prog). How do I do this?

Using the Cwd module.

>On Unix I was able to write a program which could figure this out
>from the $0 variable and the cwd() function (Cwd module). (Note I
>need the current working directory to handle situations where the
>program was called by relative path, e.g., ../some_dir/my_prog).

That's right.

>But on Windows I run into a problem: there are multiple current
>working directories, one for each disk! Let's say I am somewhere
>on the D: disk and I call my program like this: E:..\some_dir\my_prog.
>In order to find the absolute path to my program I need to know
>what directory I would be in if I changed disks by typing 'E:'.
>Is there a way to find this out without actually changing disks
>and calling the cwd() function?

Not in perl. For perl, the disk letter is part of the current directory.
There is only one current directory in perl. There is no separate
chdir() and chdrive() here.

p.s. If you want to find the absolute location of the script, use the
FindBin module.

--
Bart.

Anno Siegel

unread,
Dec 21, 2001, 8:24:27 AM12/21/01
to
According to Wiliam Stephens <w...@stephens.org>:

No one doubts that. The point was, it could be better written.

Anno

Wiliam Stephens

unread,
Dec 21, 2001, 9:48:08 AM12/21/01
to
Anno Siegel wrote:

>>>What is this supposed to do, remove newlines? Have you not heard of
>>>chomp()? Why the quotation marks around the $1 variable, it's already a
>>>string?
>>>
>>>
>>>
>>>John
>>>
>>>
>>It works :-)
>>
>
> No one doubts that. The point was, it could be better written.
>
> Anno
>


Yes, sorry. I'm just not the best Perl programmer in the world, and I am
glad that people point these things out to me in order for me to learn
and to improve my code.

It's a preety full-proof method though, isn't it?

--
Wiliam Stephens <w...@stephens.org>

John Brock

unread,
Dec 21, 2001, 1:36:10 PM12/21/01
to
In article <3c22...@news.microsoft.com>,
Jürgen Exner <jurg...@hotmail.com> wrote:

Thank you. This looks like it might do what I want, if only I
could figure out how it works.

Following the (exceedingly minimal) documentation at
http://theoryx5.uwinnipeg.ca/CPAN/data/perl/FindBin.html (which
matches the man page at my installation) I wrote the following
script:


#!/usr/local/bin/perl5

use FindBin;
use lib "$FindBin::Bin/../lib";

print "Bin = '$Bin', RealBin = '$RealBin'\n";


However when I run it I get nothing. (Bin = '', RealBin = '').

What am I doing wrong? The only thing I can think of is that the
documentation says something about a directory tree with 'bin' and
'lib' directories. I'm not doing anything like that -- my script
could be in an arbitrary directory. However when I tried putting
my script in a 'bin' directory it didn't help.

Also, the web documentation mentions perl-5.7.2. My Perl is 5.004.
Could that be the reason that FindBin doesn't work (even though we
have the code, which is dated 1995, so I guess that's not it!)?
--
John Brock
jbr...@panix.com

Slaven Rezic

unread,
Dec 21, 2001, 3:40:25 PM12/21/01
to
jbr...@panix.com (John Brock) writes:

> In article <3c22...@news.microsoft.com>,
> Jürgen Exner <jurg...@hotmail.com> wrote:
>
> >"John Brock" <jbr...@panix.com> wrote in message
> >news:9vuehq$g5j$1...@panix3.panix.com...
>
> >> I want my Perl program to be able to find its absolute location
> >> on both Windows and Unix, no matter how it is called. In particular,
> >> on Windows the path must include the disk letter (e.g.,
> >> E:\mystuff\progs\some_dir\my_prog). How do I do this?
>
> >See the FindBin module "perldoc FindBin"
>
> Thank you. This looks like it might do what I want, if only I
> could figure out how it works.
>
> Following the (exceedingly minimal) documentation at
> http://theoryx5.uwinnipeg.ca/CPAN/data/perl/FindBin.html (which
> matches the man page at my installation) I wrote the following
> script:
>
>
> #!/usr/local/bin/perl5
>
> use FindBin;
> use lib "$FindBin::Bin/../lib";
>
> print "Bin = '$Bin', RealBin = '$RealBin'\n";

This should be full-qualifies: $FindBin::Bin, $FindBin::RealBin. If
this does not help, try to print out the value of $0. If this is also
empty, then FindBin won't work.

Regards,
Slaven

--
Slaven Rezic - slaven...@berlin.de
BBBike - Routenplaner für Radfahrer in Berlin
WWW version: http://www.bbbike.de
Perl/Tk version: http://bbbike.sourceforge.net

John Brock

unread,
Dec 21, 2001, 4:53:56 PM12/21/01
to
In article <87u1ukl...@vran.herceg.de>,

Slaven Rezic <slaven...@berlin.de> wrote:
>jbr...@panix.com (John Brock) writes:

>> In article <3c22...@news.microsoft.com>,
>> Jürgen Exner <jurg...@hotmail.com> wrote:

>> >"John Brock" <jbr...@panix.com> wrote in message
>> >news:9vuehq$g5j$1...@panix3.panix.com...

>> >> I want my Perl program to be able to find its absolute location
>> >> on both Windows and Unix, no matter how it is called. In particular,
>> >> on Windows the path must include the disk letter (e.g.,
>> >> E:\mystuff\progs\some_dir\my_prog). How do I do this?

>> >See the FindBin module "perldoc FindBin"

>> Thank you. This looks like it might do what I want, if only I
>> could figure out how it works.
>>
>> Following the (exceedingly minimal) documentation at
>> http://theoryx5.uwinnipeg.ca/CPAN/data/perl/FindBin.html (which
>> matches the man page at my installation) I wrote the following
>> script:
>>
>>
>> #!/usr/local/bin/perl5
>>
>> use FindBin;
>> use lib "$FindBin::Bin/../lib";
>>
>> print "Bin = '$Bin', RealBin = '$RealBin'\n";

>This should be full-qualifies: $FindBin::Bin, $FindBin::RealBin. If
>this does not help, try to print out the value of $0. If this is also
>empty, then FindBin won't work.

Thank you, it works now. Unfortunately it turns out FindBin -- at
least the version on my installation -- doesn't do everything I
need. In particular, it fails on Windows when I execute my script
using a relative path on another drive. I fixed my program (called
testFindBin) according to your suggestion and then put it in location:

Z:\jbrock\jeb\testFindBin

While still on Z: I cd'ed to Z:\jbrock. Then I moved to C:\TEMP
and from there issued the following command:

perl Z:jeb\testFindBin

The program did execute, but it failed to return its own location.
Instead I got this response:

Cannot chdir to C:\TEMP\Z:jeb\:No such file or directory at c:/local/system/perl/lib/FindBin.pm line 162
BEGIN failed--compilation aborted at c:/local/system/perl/lib/FindBin.pm line 166.
BEGIN failed--compilation aborted at Z:jeb\testFindBin line 3.

My program worked fine on Unix and when being called in less indirect
ways on Windows. Am I still doing something wrong, or is FindBin
just not coded to handle this situation? (If not then maybe I will
try to fix it when I have some time. Hurray for Open Source!).
--
John Brock
jbr...@panix.com

R. A. Larsen

unread,
Dec 21, 2001, 5:55:09 PM12/21/01
to
jbr...@panix.com (John Brock) wrote:
>
> In article <3c22...@news.microsoft.com>,
> Jürgen Exner <jurg...@hotmail.com> wrote:

[snip]

> >See the FindBin module "perldoc FindBin"
>
> Thank you. This looks like it might do what I want, if only I
> could figure out how it works.

Use:

use FindBin qw( $Bin );
print $Bin;

or:

use FindBin;
print $FindBin::Bin;

The first example pulls $Bin into the main namespace, the second doesn't.

Regards, René
--
Using Virtual Access
http://www.vamail.com

Slaven Rezic

unread,
Dec 21, 2001, 6:22:21 PM12/21/01
to
jbr...@panix.com (John Brock) writes:

[...]


>
> Thank you, it works now. Unfortunately it turns out FindBin -- at
> least the version on my installation -- doesn't do everything I
> need. In particular, it fails on Windows when I execute my script
> using a relative path on another drive. I fixed my program (called
> testFindBin) according to your suggestion and then put it in location:
>
> Z:\jbrock\jeb\testFindBin
>
> While still on Z: I cd'ed to Z:\jbrock. Then I moved to C:\TEMP
> and from there issued the following command:
>
> perl Z:jeb\testFindBin
>
> The program did execute, but it failed to return its own location.
> Instead I got this response:
>
> Cannot chdir to C:\TEMP\Z:jeb\:No such file or directory at c:/local/system/perl/lib/FindBin.pm line 162
> BEGIN failed--compilation aborted at c:/local/system/perl/lib/FindBin.pm line 166.
> BEGIN failed--compilation aborted at Z:jeb\testFindBin line 3.
>
> My program worked fine on Unix and when being called in less indirect
> ways on Windows. Am I still doing something wrong, or is FindBin
> just not coded to handle this situation? (If not then maybe I will
> try to fix it when I have some time. Hurray for Open Source!).

Which perl version are you using on Windows? There were some bug fixes
to FindBin between 5.005 and 5.6.0, and maybe there are some more
fixes in later versions. If not, then there are more bugs :-)

Regards,
Slaven

--
Slaven Rezic - slaven...@berlin.de

babybike - routeplanner for cyclists in Berlin
handheld (e.g. Compaq iPAQ with Linux) version of bbbike
http://bbbike.sourceforge.net

John W. Krahn

unread,
Dec 21, 2001, 10:24:25 PM12/21/01
to
Wiliam Stephens wrote:
>
> Anno Siegel wrote:
>
> >>>What is this supposed to do, remove newlines? Have you not heard of
> >>>chomp()? Why the quotation marks around the $1 variable, it's already a
> >>>string?
> >>>
> >>>John
> >>
> >>It works :-)
> >
> > No one doubts that. The point was, it could be better written.
>
> Yes, sorry. I'm just not the best Perl programmer in the world, and I am
> glad that people point these things out to me in order for me to learn
> and to improve my code.
>
> It's a preety full-proof method though, isn't it?

_If_ it only runs on Windows or Unix and _if_ it's not running in a
chroot()ed environment where pwd is not available then yes it's "preety
full-proof".

Wiliam Stephens

unread,
Dec 24, 2001, 2:02:53 PM12/24/01
to
"John W. Krahn" <kra...@acm.org> wrote in message news:<3C23FCE8...@acm.org>...

> > Wiliam Stephens wrote:
> >
> > It's a preety full-proof method though, isn't it?
>
> _If_ it only runs on Windows or Unix and _if_ it's not running in a
> chroot()ed environment where pwd is not available then yes it's "preety
> full-proof".

That'll do me! Thanks!

Wil

0 new messages