DESCRIPTION

Many modules or applications maintain a user-spec configuration data directory. And the implementation pattern is generally the same.

A directory like /home/myuser/.application is created and populating by a set of default files the first time an application runs, and from there on, the files in that directory are modified.

\*(C`File::UserConfig\*(C' provides standard, light and sub-classable default implementation of this concept that Does The Right Thing with the directory names.

Applying Perl Conventions

\*(C`File::UserConfig\*(C' applies and automates the following conventions.

1. We are using the distribution name?

The use of \*(C`File::ShareDir\*(C' is based on distribution name (more on that later) so we need to know it.

The \s-1CPAN\s0 convention is for a dist to be named \*(C`Foo-Bar\*(C' after the main module \*(C`Foo::Bar\*(C' in the distribution, but sometimes this varies, and sometimes you will want to call \*(C`File::UserConfig\*(C' from other than the main module. But unless you say otherwise, \*(C`File::UserConfig\*(C' will assume that if you call it from \*(L"Module::Name\*(R", that is probably the main module, and thus your dist is probably called \*(L"Module-Name\*(R".

2. What config directory name is used

On platforms which keep application-specific data in its own directory, well away from the data the user actually create themself, we just use the dist name.

On Unix, which has a combined home directory, we remap the dist name to be a lowercase hidden name with all '-' chars as '_'.

So on unix only, \*(L"Module::Name\*(R" will become \*(L".module_name\*(R". Most of the time, this will end up what you would have used anyway.

3. Where does the config directory live

\*(C`File::UserConfig\*(C' knows where your home directory is by using File::HomeDir. And more specifically, on platforms that support application data being kept in a subdirectory, it will use that as well.

On Unix, Windows, and Mac \s-1OS\s0 X, it will just Do The Right Thing.

4. Where do the defaults come from?

The ability for a distribution to provide a directory full of default files is provided in Perl by File::ShareDir.

Of course, we're also assuming you are using Module::Install so you have access to its \*(C`install_share\*(C' command, and that the only thing your dist is going to install to it will be the default config dir.

METHODS

The 6 accessors all feature implicit constructors.

In other words, the two following lines are equivalent.

# Explicitly $configdir = File::UserConfig->new( ... )->configdir;

# Auto-construction $configdir = File::UserConfig->configdir( ... );

# Thus, using all default params we can just $configdir = File::UserConfig->configdir;

new

my $config = File::UserConfig->new( dist => 'Not-This-Class', dirname => '.myconfig', sharedir => 'defaultconfig', homedir => $username, );

The \*(C`new\*(C' constructor takes a set of optional named params, and finds the user's configuration directory, creating it by copying in a default directory if an existing one cannot be found.

In almost every case, you will want to use all the defaults and let everything be determined automatically for you. The sample above tries to show some of the limited number of situations in which you might want to consider providing your own values.

But most times, you don't want to or need to. Try it without params first, and add some params if it isn't working for you.

If you want to do some custom actions after you copy in the directory, the subclass and add it after you call the parent \*(C`new\*(C' method.

Returns a new \*(C`File::UserConfig\*(C' object, or dies on error.

dist

$name = File::UserConfig->new(...)->dist;

$name = File::UserConfig->dist(...);

The \*(C`dist\*(C' accessor returns the name of the distribution.

module

$name = File::UserConfig->new(...)->module;

$name = File::UserConfig->module(...);

The \*(C`module\*(C' accessor returns the name of the module.

Although the default dirname is based off the dist name, the module name is the one used to find the shared dir.

dirname

$dir = File::UserConfig->new(...)->dirname;

$dir = File::UserConfig->dirname(...);

The \*(C`dirname\*(C' accessor returns the name to be used for the config directory name, below the homedir. For example '.foo_bar'.

sharedir

$dir = File::UserConfig->new(...)->sharedir;

$dist = File::UserConfig->sharedir(...);

The \*(C`sharedir\*(C' accessor returns the name of the directory where the shared default configuration is held.

Returns a path string, verified to exist before being returned.

homedir

$dir = File::UserConfig->new(...)->homedir;

$dist = File::UserConfig->homedir(...);

The \*(C`homedir\*(C' accessor returns the location of the home direcotry, that the config dir will be created or found below.

Returns a path string, verified to exist before being returned.

configdir

$dir = File::UserConfig->new(...)->configdir;

$dist = File::UserConfig->configdir(...);

The \*(C`sharedir\*(C' accessor returns the name of the directory where the shared default configuration is held.

Returns a path string, verified to exist before being returned.

SUPPORT

Bugs should always be submitted via the \s-1CPAN\s0 bug tracker

<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-UserConfig>

For other issues, contact the maintainer

AUTHOR

Adam Kennedy <[email protected]>

RELATED TO File::UserConfig…

File::HomeDir, File::ShareDir

COPYRIGHT

Copyright 2006 - 2008 Adam Kennedy.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the \s-1LICENSE\s0 file included with this module.