Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(OT) cross-platform $HOME

by thraxil (Prior)
on Feb 23, 2004 at 04:22 UTC ( [id://331019]=perlquestion: print w/replies, xml ) Need Help??

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

i'm making a little app that i want to work cross-platform. making the code cross-platform so far isn't a problem.

what i'm not sure how to handle at the moment is a matter of convention. the app needs to store settings and state between runs. my problem is figuring out where to store these files. on any unix system, this is a no-brainer. you just create a directory like '~/.myapp/' and stick anything you need in there.

i am utterly unfamiliar with Windows conventions though. where would a good windows app store its config files? similarly, while using '~/.myapp/' should work on OS X, is it considered good form or is there a better place for it?

Replies are listed 'Best First'.
Re: (OT) cross-platform $HOME
by NetWallah (Canon) on Feb 23, 2004 at 04:38 UTC
    On Windows, you could look for $ENV{USERPROFILE}, and store the info in "$ENV{USERPROFILE}\\Application Data\\myapp" - that is a convention many MS apps follow.

    The "Application Data" folder is usually hidden, so it already captures the meaning of the leading ".".

    Many 3'rd party apps in Windows store data either in the App directory, or a sub-directory. Small quantities of data and config info is stored in the Registry, but you probably don't want to touch that if you are writing a cross-platform app. In your case I'd recommend the "Application Data" directory above.

    "Experience is a wonderful thing. It enables you to recognize a mistake when you make it again."

      On XP, you can short-circuit the route to the Application Data directory using $ENV{ APPDATA }, but I'm not sure when (which version) this became available. It wasn't there on NT4, so I think your suggestion is probably better if backwards compatiblity is a concern.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Timing (and a little luck) are everything!

        ++BrowserUK. This approach is also better because it supports locales. The application data directory is not always called "Application Data" (e.g. in the German locale it's called "Anwendungsdateien").

        --
        Allolex

Re: (OT) cross-platform $HOME
by b10m (Vicar) on Feb 23, 2004 at 12:01 UTC

    I recently asked the same thing and based on the answers I found that CSIDL_APPDATA (with Win32::GetFolderPath()) seems to work pretty decent on win32 systems. I've tried it with win98 and winXP.

    --
    b10m

    All code is usually tested, but rarely trusted.
use User;
by PodMaster (Abbot) on Feb 23, 2004 at 13:04 UTC
    perldoc User
    NAME
        User - API for locating user information regardless of OS
    
    update: The only time I don't use User is when writing wxPerl apps, because wxWindows has it's own api to deal with that.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: (OT) cross-platform $HOME
by eyepopslikeamosquito (Archbishop) on Feb 23, 2004 at 06:58 UTC

    I don't think using the $HOME environment variable is a no-brainer on Unix at all -- though it may be fine for simple applications without high security requirements. Remember, $HOME is just an environment variable, and the user may set it to whatever he/she wants. Worse, it may be incorrectly set when running a script in unusual environments -- via su, for example, I've sometimes seen $HOME incorrectly set to root's home directory. Finally, some systems may use $LOGDIR instead of $HOME (admittedly, this is very rare).

    Also, '~/.myapp' is shell-specific which is not very portable, failing on many versions of /bin/sh, for instance.

    If the whole application is written Perl, I'd look at using the getpwuid/getpwnam functions on Unix. The Perl $< and $> special variables and the getlogin function may also prove handy.

Re: (OT) cross-platform $HOME
by Anonymous Monk on Sep 16, 2009 at 11:21 UTC
Re: (OT) cross-platform $HOME
by BUU (Prior) on Feb 23, 2004 at 06:12 UTC
    As a windows users, it makes the most sense to store whatever data you want to store in the same folder as the script itself is stored in, at least by default. Most applications for windows work this way for default, so it's expected.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://331019]
Approved by calin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found