Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Windows convention for per-user configuration files for a Perl application

by princepawn (Parson)
on Oct 16, 2000 at 21:30 UTC ( [id://36976]=perlquestion: print w/replies, xml ) Need Help??

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

I am using the excellent Config::IniFiles module to develop an FTP application,

The application will be used on Windows and Unix. Thanks to Net::FTP, the FTP part is achitecture-independent already.

Under Unix, I can simply require the users to have the file in their HOME directory and simply read $ENV{HOME} for the location of the config file.

But, how and where should configuration files be kept that will vary on a per-user basis under Windows?

  • Comment on Windows convention for per-user configuration files for a Perl application

Replies are listed 'Best First'.
Re: Windows convention for per-user configuration files for a Perl application
by the_slycer (Chaplain) on Oct 16, 2000 at 23:11 UTC
    As jcwren says $SYSTEMROOT on NT works fine, for 2000 you will want to make sure that you substitue $SYSTEMROOT with "Documents and Settings". They have changed the location of profiles with 2k. Probably the best way to do this on 2k (and quite possibly NT - I don't have a box here to try) is to forego the $ENV{USERNAME} and snag $ENV{USERPROFILE} instead. This will point to the correct path for the profile. Again, not sure about win 95/98 machines.

    HTH
      That variable is supported on NT 4.0, and is a better way to do it. I'm really annoyed at myself for missing that, too!.

      An even more important reason that this way is better is that although the user name is known, it's possible for one or more profiles to exist for a user. In my case, I have 'jcw' and 'jcw.000' as profiles on the machine, and 'jcw.000' is the active profile. If I were to use the username of 'jcw' to select where to write the profile, I would be writing it into the wrong directory.

      Excellent call, the_slycer!

      --Chris

      e-mail jcwren

        But you don't select the username "jcw". You just write to and read from HKEY_CURRENT_USER!

        Now, if you wanted to set per-user items in the Win32 registry for other users, then I'd say "Give up".

        Note that I'm not advocating (nor discouraging) the use of the Registry for stuff like this. I find it a "mixed blessing" and have arguments on both sides.

                - tye (but my friends call me "Tye")
        Every time that my NT box dies and the IT guys rebuild it my user profile directory changes...

        From a DOS window:

        C:\>echo My name is: %USERNAME% My home is %USERPROFILE% My name is: ddickinson My home is C:\WINNT\Profiles\ddickinson.002

        All the best,
        DouglasDD

(jcwren) Re: Windows convention for per-user config files for a Perl app
by jcwren (Prior) on Oct 16, 2000 at 21:40 UTC
    Technically, you should be using the registry, if you want to be truly Windows.

    I hate the registry, and it's one of the dumbest ideas to come along in a very long time from Micro$oft.

    That being said, you can determine the current logged in user from the environment ($USERNAME). Under NT, I would place it in "$SYSTEMROOT\Profiles\$USERNAME\Application Data\YourAppName". I don't know about Windows 95/98, but I think they have something similiar.

    --Chris

    e-mail jcwren
Re: Windows convention for per-user configuration files for a Perl application
by Hyler (Sexton) on Oct 17, 2000 at 14:41 UTC
    If you're on an NT domain, you have the environment variables HOMEDRIVE, HOMEPATH and HOMESHARE, works in both NT4 and 2000. I think you have those on 95/98 too but I'm not sure. However, profiles aren't the same on 9x and NT, so I wouldn't use $ENV{USERPROFILE}.
User.pm: a Perl module for OS-independent per-user configuration
by princepawn (Parson) on Oct 18, 2000 at 16:44 UTC
    The module can be obtained from User.

    <HEAD> <TITLE>User - API for locating user information regardless of OS</TITLE> <LINK REV="made" HREF="mailto:cwilson@ece.gatech.edu"> </HEAD> <BODY>


    NAME

    User - API for locating user information regardless of OS


    SYNOPSIS

      use User;
      my $cfg = Config::IniFiles->new
            (
              -file    => sprintf("%s/%s", User->Home, ".ncfg"),
              -default => 'Default'
            );


    DESCRIPTION

    This module is allows applications to retrieve per-user characteristics. At present, it contains only one method, Home(), which is used to return a location that can be expected to be a users ``Home'' directory on either Windows or Unix.

    While one way of writing this would be to check for operating system and then check the expected location for an operation system of that type, I chose to do the following:

     sub Home {
      return $ENV{HOME}        if $ENV{HOME};
      return $ENV{USERPROFILE} if $ENV{USERPROFILE};
      return  "";
     }

    In other words, if $HOME is defined in the user's environment, then that is used. Otherwise $USERPROFILE is used. Otherwise ``'' is returned.

    A contribution for Macintosh (or any other number of OS/arch combinations) is greatly solicited.

    EXPORT

    None by default.


    AUTHOR

    T.M. Brannon, tbone@cpan.org


    ACKNOWLEDGEMENTS

    I would like to offer profuse thanks to my fellow perl monk at www.perlmonks.org, the_slycer, who told me where HOME could be found on Windows machines.

    perl(1).

    </BODY> </HTML>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found