http://www.perlmonks.org?node_id=938704


in reply to -e on windows giving error

my objective here is to get the drive which my perl script lies in,

If you inspect the built-in variable, $0 (that's zero not oh), it will tell you the full path of the current script including the drive. Eg:

print $0;; C:\Perl64\bin\p1.pl

So, my( $drive ) = $0 =~ m[^(.)]; will give you the drive letter directly.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: -e on windows giving error
by Rudolf (Pilgrim) on Nov 17, 2011 at 23:30 UTC
    Oh man, thank you so much everyone! I have found my solution and learned a lot today, Im new to perlmonks and already find the community very welcoming and helpfull! thanks and best wishes, Rudy

      If for some obscure reason you do not use file associations to invoke your scripts, and are in the habit of using tortuous relative path constructions to find them, then a simple step would sort out the mess you (probably didn't) create:

      c:\test>perl ..\tmp\..\test\..\Perl64\site\.\lib\..\..\bin\p1.pl Perl> print scalar Win32::GetFullPathName( $0 );; c:\Perl64\bin\p1.pl

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The solution BrowserUk rarely works. It requires an absolute path when launching the script (common for some, rare for others), and then only if the absolute path starts with something of the form "D:" (extremely common).

      I've posted a more robust version here. It handles relatives paths, and even more exotic drive volume names.

        The solution BrowserUk rarely works. It requires an absolute path when launching the script

        No it does not!

        C:\Perl64\bin>p1 [0] Perl> print $0;; C:\Perl64\bin\p1.pl [0] Perl> Terminating on signal SIGINT(2) C:\Perl64\bin>.\p1.pl [0] Perl> print $0;; C:\Perl64\bin\p1.pl [0] Perl> Terminating on signal SIGINT(2) C:\Perl64\bin>cd \test C:\test>p1 [0] Perl> print $0;; C:\Perl64\bin\p1.pl

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.