Win32::TieRegistry

Discussion in 'Perl Misc' started by January Weiner, Mar 3, 2006.

  1. Hello,

    I am writing a program which I would like to distribute both for Unix-like
    and Windows platforms. In Unix, the program reads a global configuration
    from /etc/programnamerc, and from the user configuration file
    ~/.programnamerc. I don't care about the global configuration, but the
    user should be able to save hers or his configuration in Windows, too.

    As far as I understand it (maybe I'm wrong?) I cannot be sure about a thing
    like $HOME. I could theoretically save the user configuration at the same
    place as where the program sits; however, I specifically created the
    program in such a way that the single binary is everything that is needed
    to run it, so that it can be easily moved around.

    I thought that I might use the Win32::TieRegistry module. I have read its
    documentation. However, I am quite ignorant about Windows, and I am afraid
    that tampering the registry without understanding how exactly it works
    might break things. Are you aware of any tutorial / faq / explanation how
    to do such a basic thing as store a couple of user preferences using perl
    and win32::TieRegistry?

    cheers,
    January

    --
    January Weiner, Mar 3, 2006
    #1
    1. Advertising

  2. January Weiner <> wrote in
    news:du9e7t$4ri$-muenster.de:

    > I am writing a program which I would like to distribute both for
    > Unix-like and Windows platforms. In Unix, the program reads a global
    > configuration from /etc/programnamerc, and from the user configuration
    > file ~/.programnamerc. I don't care about the global configuration,
    > but the user should be able to save hers or his configuration in
    > Windows, too.
    >
    > As far as I understand it (maybe I'm wrong?) I cannot be sure about a
    > thing like $HOME. I could theoretically save the user configuration
    > at the same place as where the program sits; however, I specifically
    > created the program in such a way that the single binary is everything
    > that is needed to run it, so that it can be easily moved around.
    >
    > I thought that I might use the Win32::TieRegistry module. I have read
    > its documentation. However, I am quite ignorant about Windows, and I
    > am afraid that tampering the registry without understanding how
    > exactly it works might break things. Are you aware of any tutorial /
    > faq / explanation how to do such a basic thing as store a couple of
    > user preferences using perl and win32::TieRegistry?


    THe information you are seeking is not Perl or Win32::TieRegistry
    specific. You might want to visit MSDN for that.

    However, you don't need the registry to do what you want. I would
    recommend the following:

    1. Check if $ENV{HOME} is defined. If it is, use that directory.

    2. If not, check if $ENV{APPDATA} and $ENV{USERNAME} are defined. If it
    is, then create an application and user specific directory under that
    path: Such as

    $ENV{APPDATA}\myapp\$ENV{USERNAME}

    and save the settings there

    3. If both of the above fail (very unlikely on recent systems, but still
    possible), ask the user where the configuration file should be placed.
    Or, just create a user specific subdirectory in the program directory.
    Place the config file there.

    Such a scheme ought to be usable.

    If you do want to go the registry route, you want to use

    HKEY_CURRENT_USER -> Software -> myapp

    HTH.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Mar 3, 2006
    #2
    1. Advertising

  3. A. Sinan Unur <> wrote:
    > However, you don't need the registry to do what you want. I would
    > recommend the following:


    > 1. Check if $ENV{HOME} is defined. If it is, use that directory.


    > 2. If not, check if $ENV{APPDATA} and $ENV{USERNAME} are defined. If it
    > is, then create an application and user specific directory under that
    > path: Such as


    > $ENV{APPDATA}\myapp\$ENV{USERNAME}


    Thanks! Just one quick question. On a Windows system I can access, this
    variable shows a path that includes already my username; that is, it shows
    the path to my personal APPDATA. This is not standard by any chance?

    > and save the settings there


    > 3. If both of the above fail (very unlikely on recent systems, but still
    > possible), ask the user where the configuration file should be placed.
    > Or, just create a user specific subdirectory in the program directory.


    OK, thanks for your answer! I would really prefer this way, as I can then
    directly use the subroutines I have already written.

    j.

    --
    January Weiner, Mar 3, 2006
    #3
  4. January Weiner <> wrote in
    news:du9haj$4ri$-muenster.de:

    > A. Sinan Unur <> wrote:
    >> However, you don't need the registry to do what you want. I would
    >> recommend the following:

    >
    >> 1. Check if $ENV{HOME} is defined. If it is, use that directory.

    >
    >> 2. If not, check if $ENV{APPDATA} and $ENV{USERNAME} are defined. If
    >> it is, then create an application and user specific directory under
    >> that path: Such as

    >
    >> $ENV{APPDATA}\myapp\$ENV{USERNAME}

    >
    > Thanks! Just one quick question. On a Windows system I can access,
    > this variable shows a path that includes already my username; that is,
    > it shows the path to my personal APPDATA. This is not standard by any
    > chance?


    There may be Win98 or ME systems where $ENV{APPDATA} is not user specific.
    I don't know if $NEV{USERNAME} works on those systems, though.

    HTH.

    Sinan
    A. Sinan Unur, Mar 3, 2006
    #4
  5. A. Sinan Unur schrieb:
    > January Weiner <> wrote in
    > news:du9haj$4ri$-muenster.de:
    >
    >
    >>A. Sinan Unur <> wrote:
    >>
    >>>However, you don't need the registry to do what you want. I would
    >>>recommend the following:

    >>
    >>>1. Check if $ENV{HOME} is defined. If it is, use that directory.

    >>
    >>>2. If not, check if $ENV{APPDATA} and $ENV{USERNAME} are defined. If
    >>>it is, then create an application and user specific directory under
    >>>that path: Such as

    >>
    >>>$ENV{APPDATA}\myapp\$ENV{USERNAME}

    >>


    I think that $ENV{'USERPROFILE'} is also worth checking.
    But please let me recommend yet another way (if the module dependency is ok with
    the OP):

    http://search.cpan.org/~adamk/File-HomeDir-0.54/lib/File/HomeDir.pm

    gives you a very simple yet reliable way to cope with $HOME-related tasks.

    HTH,
    Andreas Pürzer

    --
    Have Fun,
    and if you can't have fun,
    have someone else's fun.
    The Beautiful South
    Andreas Pürzer, Mar 3, 2006
    #5
  6. Andreas Pürzer <> wrote:
    > I think that $ENV{'USERPROFILE'} is also worth checking.
    > But please let me recommend yet another way (if the module dependency is ok with
    > the OP):


    > http://search.cpan.org/~adamk/File-HomeDir-0.54/lib/File/HomeDir.pm


    > gives you a very simple yet reliable way to cope with $HOME-related tasks.


    Great! This one is perfect. I do not have to care about the intricates of
    a Windows system myself.

    j.

    --
    January Weiner, Mar 6, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    165
    darth
    Mar 10, 2005
  2. Replies:
    3
    Views:
    84
  3. Replies:
    6
    Views:
    213
    Paul Lalli
    Jun 1, 2006
  4. MoshiachNow
    Replies:
    0
    Views:
    89
    MoshiachNow
    Nov 21, 2006
  5. Cosmic Cruizer

    Getting permissions using Win32::TieRegistry

    Cosmic Cruizer, Mar 9, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    154
    Cosmic Cruizer
    Mar 17, 2008
Loading...

Share This Page