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
See the FindBin module "perldoc FindBin"
jue
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
--
use Perl;
program
fulfillment
>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.
No one doubts that. The point was, it could be better written.
Anno
>>>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>
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
> 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
>> 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
[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
[...]
>
> 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
_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