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

I'm looking for a way a Perl program can determine the current user's home directory. Most OS's I've tried this on seem to store the information in an environment variable, but I haven't yet figured out a way to get this reliably across different OS's. Specifically I'm looking at Windows 2000, Windows XP, Linux, and Solaris. Any thoughts?
  • Comment on cross-platform way to get home directory

Replies are listed 'Best First'.
Re: cross-platform way to get home directory
by borisz (Canon) on May 15, 2006 at 19:50 UTC
Re: cross-platform way to get home directory
by Herkum (Parson) on May 15, 2006 at 19:38 UTC

    Windows does not have an explicit, HOMEDIR variable. It has other variables like USERPROFILE, HOMEDRIVE, HOMEPATH, and APPDATA. Windows stores most things in the Registry, especially for application.

    You need to describe what you are trying to achieve rather than tell us how you are going to do it.

Re: cross-platform way to get home directory
by Corion (Patriarch) on May 15, 2006 at 19:51 UTC

    There is File::HomeDir, which provides and discusses the various directories in which stuff is stored.

Re: cross-platform way to get home directory
by davidrw (Prior) on May 15, 2006 at 19:54 UTC
Re: cross-platform way to get home directory
by samtregar (Abbot) on May 15, 2006 at 19:34 UTC
    Windows lacks a meaningful home-directory concept. On UNIX systems $ENV{HOME} is the gold-standard.

    -sam

      Windows does have a home dir, but that's not where you want to place application settings

      Application settings should go in a subdir of the directory specified by registry value
      HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Local Settings
      The subdir should be of the form <company name>\<application name>.

      Application data should go in a subdir of the directory specified by registry value
      HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Local AppData
      The subdir should be of the form <company name>\<application name>.

      Replace %USERPROFILE% with the value of $ENV{USERPROFILE}.

        Windows may have something called a home directory, but it's basically useless. It doesn't have any of the useful meaning that $ENV{HOME} has on Unix.

        -sam

      Not doubting you, but just one example.

      My XP system was setup (either automatically or by a human) with

      HOMEDRIVE=d: HOMEPATH=\Documents and Settings\myusername
      I should go add
      HOME=%HOMEDRIVE%%HOMEPATH
      though sweeping under the rug obvious problems with multiple users and delayed evaluation.

      For the OP, it may help to dictate to his/her users that $ENV{HOME} be set correctly.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of