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

I'm writing a script and will package it using PP to distribute to users that don't have Perl installed on their PCs.

The script should save a file in the current directory. This is all well and good if I run it from the command prompt, but if I were to double click it after compiling it into an executable, it will then save it to "C:\Documents and Settings\User\".

I've tried using getcwd to get the current directory, but it appears to be getting the current WORKING directory, so instead of "C:\Perlworks" it still gets "C:\Documents and Settings\User\".

I suppose I could hard-code the save directory, but this seems a bit inelegant. Any ideas?

Replies are listed 'Best First'.
Re: Current Directory in Windows
by Corion (Patriarch) on Mar 09, 2012 at 14:39 UTC

    The "current directory" is not really defined when you start a program via double-clicking. Maybe you want to look at what File::HomeDir or $ENV{USERPROFILE} give you?

      The "current directory" is not really defined when you start a program via double-clicking.

      Actually it always appears to be %userprofile%

      so says http://live.sysinternals.com/procexp.exe

      The OP seems confused by about what cwd means, seems OP wants the directory where .exe is located, meaning

      BEGIN { use File::Spec; use File::Basename qw' dirname '; our $thisf = File::Spec->rel2abs(__FILE__); $thisf ||= File::Spec->rel2abs( $0 ); our $thisd = dirname($thisf); }
        bah
        #!/usr/bin/perl -- BEGIN { use File::Spec; use File::Basename(); our $thisf = File::Spec->rel2abs( $0 ); our $thisd = File::Basename::dirname($thisf); }
Re: Current Directory in Windows
by BrowserUk (Patriarch) on Mar 10, 2012 at 13:38 UTC

    Maybe this will help you?

    c:\>type test\junk.pl print $^X, "\n", $0; c:\>perl test\junk.pl C:\Perl64\bin\perl.exe test\junk.pl

    If you extract the path from $^X that will tell you where the executable (perl.exe) was run from.

    And if you add the path component from $0 the cwd, that will tell you where the script was run from.

    There are probably some exceptional circumstances where one or other of those won't be true, but they are rare enough to discount for most purposes.


    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 start of some sanity?

Re: Current Directory in Windows
by Marshall (Canon) on Mar 09, 2012 at 17:53 UTC
    I'm writing a script and will package it using PP to distribute to users that don't have Perl installed on their PCs.

    So it sounds to me like you have created single .exe application. You need to make a .MSI file (Microsoft Installer). MSI file.

    This will install this application just like any other .exe file. That is is Perl, makes no difference. Its just another .exe file that may live in %Program Files% or wherever.

    It depends upon how sophisticated you users are. I distribute programs all the time as just .zip files (including .exe's made from PerlApp) with a half page description of what they need to do. Mileage varies a lot! Use a .MSI file for the inexperienced.

      I'm curious as to how a .msi will help here. I mean, as far as I can see, whether I install it using a .msi file or just pass the .exe file to the user, it won't change the fact that I cam getting the current WORKING directory and not the current directory as I wanted. Can you please elaborate?

      And kudos to the two Anonymous monks, that was exactly what I was looking for. Sorry if the query was vague....

        The .MSI provides an idiot gui to install things.

        I distribute my code in .zip files with instructions in a ReadMe file. Mileage varies.

        more complex things are required if you need to modify the path or the Windows Registry and become one of the programs that Windoze can uninstall.

        There is no need to make a Microsoft Installer if all you have is a single .exe file.

      You need to make a .MSI file

      Why?

      A reply falls below the community's threshold of quality. You may see it by logging in.