davemabe has asked for the wisdom of the Perl Monks concerning the following question:

DOS has a %0\..\ syntax that is frequently used in logon scripts to mean "the directory that you are executing the logon script from." It is used because of the inconsistency of the cwd when running NT logon scripts. Therefore in a batch file this command:
type %0\..\legal.txt
prints out the legal notice from the directory where the user happens to be running the logon script from. I would like to make this syntax work in perl but a simple  open(FILE,"\%0\\..\\legal.txt"); doesn't work. I realize there is a $ENV{LOGONSERVER} variable that could work, but due to various reasons, that variable doesn't get set in all circumstances. Any ideas?

Dave

Replies are listed 'Best First'.
(tye)Re: DOS's %0\..\ Perl Equivalent
by tye (Sage) on Jan 19, 2001 at 22:46 UTC

    The Perl equivalent to DOS's %0 is $0 (both having derived from $0 in Unix shells). But "$0/../legal.txt" doesn't work on most operating systems. However, if you know you'll only be running under Win32, then you can actually just use that (or replace the "/"s with "\\"s).

    If you want your code to be portable to other operating systems then to strip the script name from $0 you should use the File::Spec module or, if you need to work with even slightly old versions of Perl, use the File::Basename module.

            - tye (but my friends call me "Tye")
Re: DOS's %0\..\ Perl Equivalent
by mr.nick (Chaplain) on Jan 19, 2001 at 22:23 UTC
    Do you wish to get the directory in which a script is being executed FROM regardless of what the current working directory? If so, then $0 may be your savior.

    if ($0=~/^(.+)\/.*/) { $dir=$1; } else { $dir="."; }

    I'm sure there is a File::* module that will return the path (opposite of Basename), but it's real easy to roll your own. Of course this method doesn't account for Windows reverse directory seperators; but I'll leave that implementation as a exercise to the reader.

      I guess the opposite of File::Basename is File::Basename? In addition to the basename() method, the module includes dirname() for getting the directory path and fileparse() for getting all the pieces of a path at once.
Re: DOS's %0\..\ Perl Equivalent
by epoptai (Curate) on Jan 20, 2001 at 02:35 UTC
    FindBin does it too.
    #!/usr/bin/perl use FindBin qw($Bin); print qq(Welcome to $Bin);

      ...badly. (:

              - tye (but my friends call me "Tye")
Re: DOS's %0\..\ Perl Equivalent
by the_slycer (Chaplain) on Jan 19, 2001 at 21:51 UTC
    Use Cwd..
    use Cwd; $dir=getcwd; print $dir;
      I just tried your suggestion. The current working directory is not set to the share where the script is running from. This is the problem that %0\..\ resolves. Various Win32 variants set the cwd differently when running logon scripts, but %0\..\ is the same directory on all of them...

      Dave