Notes on obtaining $0 on Windows and Cygwin: $0 has trickery to it.

I am not sure of the real purpose of $0, but I have found it to be
unreliable in that what it yeilds depends too much on how the script was
invoked, and also on what OS it is running on.

For the purpose of discussion, assume there is the perl file path.pl that
creates the file dollar_zero.txt that holds whatever $0 was when path.pl was
running.  It is pasted further below.

On Windows XP Pro, if path.pl is called with "perl path.pl" then $0 will
contain "path\path.pl", where path starts with whatever directory we were in
at the time.  However, if it is
called with just "path\path.pl" relying on the windows perl association,
then $0 will contain the fully qualified path name.

Cygwin is different, too.  $0 in cygwin seems to be only the relative path
from the current directory.

I have not investigated what $0 yeilds on other OS machines, but I would be
wary of differences there, too.

------------------------------------begin cygwin paste

$ cat path.pl
my $file = 'c:\Documents and Settings\malcolm\My
Documents\proj_pp_testing\dollar_zero.txt';

open (FH, ">$file") or die("Cannot open file $file:$!:\n");
print FH $0;
close(FH) or die("Cannot close file $file after writing:$!:\n");
$ perl path.pl
$ cat dollar_zero.txt
path.pl
cd ..
$ pwd
/cygdrive/c/Documents and Settings/malcolm/My Documents
$ perl proj_pp_testing/path.pl
$ cat proj_pp_testing/dollar_zero.txt
proj_pp_testing/path.pl

------------------------------------end cygwin paste
-----------------------------------begin Windows XP command line pastes
C:\Documents and Settings\malcolm\My Documents\proj_pp_testing>type path.pl
my $file = 'c:\Documents and Settings\malcolm\My
Documents\proj_pp_testing\dollar_zero.txt';

open (FH, ">$file") or die("Cannot open file $file:$!:\n");
print FH $0;
close(FH) or die("Cannot close file $file after writing:$!:\n");

C:\Documents and Settings\malcolm\My Documents\proj_pp_testing>perl path.pl

C:\Documents and Settings\malcolm\My Documents\proj_pp_testing>type
dollar_zero.txt
path.pl
C:\Documents and Settings\malcolm\My Documents\proj_pp_testing>path.pl

C:\Documents and Settings\malcolm\My Documents\proj_pp_testing>type
dollar_zero.txt
C:\Documents and Settings\malcolm\My Documents\proj_pp_testing\path.pl

C:\Documents and Settings\malcolm\My Documents\proj_pp_testing>cd ..
C:\Documents and Settings\malcolm\My Documents>perl proj_pp_testing\path.pl
C:\Documents and Settings\malcolm\My Documents>type
proj_pp_testing\dollar_zero.txt
proj_pp_testing\path.pl
C:\Documents and Settings\malcolm\My Documents>proj_pp_testing\path.pl
C:\Documents and Settings\malcolm\My Documents>type
proj_pp_testing\dollar_zero.txt
C:\Documents and Settings\malcolm\My Documents\proj_pp_testing\path.pl

If you are interested, if path.pl is run as a windows service, with of
course the perl path association in place, $0 evaluates to the fully
qualified path name.

--------------------------------end paste Windows XP

----- Original Message ----- 
From: "Autrijus Tang" <[EMAIL PROTECTED]>
To: "Murphy" <[EMAIL PROTECTED]>
Cc: "Par Maillist" <[EMAIL PROTECTED]>
Sent: Wednesday, May 26, 2004 6:54 AM
Subject: Re: $0 does not contain a full path to the script


Reply via email to