File-ConfigDir

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

place of the resulting path.

## system\_cfg\_dir

Returns the configuration directory where configuration files of the
operating system resides. For Unices this is `/etc`, for MSWin32 it's
the value of the environment variable `%windir%`.

## machine\_cfg\_dir

Alias for desktop\_cfg\_dir - deprecated.

## xdg\_config\_dirs

Alias for desktop\_cfg\_dir

## desktop\_cfg\_dir

Returns the configuration directory where configuration files of the
desktop applications resides. For Unices this is `/etc/xdg`, for MSWin32
it's the value of the environment variable `%ALLUSERSPROFILE%`
concatenated with the basename of the environment variable `%APPDATA%`.

## core\_cfg\_dir

Returns the `etc` directory below `$Config{prefix}`.

## site\_cfg\_dir

Returns the `etc` directory below `$Config{sitelib_stem}` or the common

README.md  view on Meta::CPAN


Returns the path for the `etc` directory below the current working directory.

## user\_cfg\_dir

Returns the users home folder using [File::HomeDir](https://metacpan.org/pod/File::HomeDir). Without
File::HomeDir, nothing is returned.

## xdg\_config\_home

Returns the user configuration directory for desktop applications.
If `$ENV{XDG_CONFIG_HOME}` is not set, for MSWin32 the value
of `$ENV{APPDATA}` is return and on Unices the `.config` directory
in the users home folder. Without [File::HomeDir](https://metacpan.org/pod/File::HomeDir), on Unices the returned
list might be empty.

## config\_dirs

    @cfgdirs = config_dirs();
    @cfgdirs = config_dirs( 'appname' );

lib/File/ConfigDir.pm  view on Meta::CPAN


<a href="https://travis-ci.org/perl5-utils/File-ConfigDir"><img src="https://travis-ci.org/perl5-utils/File-ConfigDir.svg?branch=master" alt="Travis CI"/></a>
<a href='https://coveralls.io/github/perl5-utils/File-ConfigDir?branch=master'><img src='https://coveralls.io/repos/github/perl5-utils/File-ConfigDir/badge.svg?branch=master' alt='Coverage Status'/></a>

=end html

=cut

$VERSION   = '0.021';
@EXPORT_OK = (
    qw(config_dirs system_cfg_dir desktop_cfg_dir),
    qw(xdg_config_dirs machine_cfg_dir),
    qw(core_cfg_dir site_cfg_dir vendor_cfg_dir),
    qw(locallib_cfg_dir local_cfg_dir),
    qw(here_cfg_dir singleapp_cfg_dir vendorapp_cfg_dir),
    qw(xdg_config_home user_cfg_dir)
);
%EXPORT_TAGS = (
    ALL => [@EXPORT_OK],
);

lib/File/ConfigDir.pm  view on Meta::CPAN

sub system_cfg_dir
{
    my @cfg_base = @_;
    1 < scalar(@cfg_base)
      and croak "system_cfg_dir(;\$), not system_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
    $system_cfg_dir->(@cfg_base);
}

=head2 machine_cfg_dir

Alias for desktop_cfg_dir - deprecated.

=head2 xdg_config_dirs

Alias for desktop_cfg_dir

=head2 desktop_cfg_dir

Returns the configuration directory where configuration files of the
desktop applications resides. For Unices this is C</etc/xdg>, for MSWin32
it's the value of the environment variable C<%ALLUSERSPROFILE%>
concatenated with the basename of the environment variable C<%APPDATA%>.

=cut

my $desktop_cfg_dir = sub {
    my @cfg_base = @_;
    my @dirs;
    if ($^O eq "MSWin32")
    {
        my $alluserprof = $ENV{ALLUSERSPROFILE};
        my $appdatabase = File::Basename::basename($ENV{APPDATA});
        @dirs = (File::Spec->catdir($alluserprof, $appdatabase, @cfg_base));
    }
    else
    {

lib/File/ConfigDir.pm  view on Meta::CPAN

            @dirs = map { File::Spec->catdir($_, @cfg_base) } @dirs;
        }
        else
        {
            @dirs = (File::Spec->catdir("/etc", "xdg", @cfg_base));
        }
    }
    @dirs;
};

sub desktop_cfg_dir
{
    my @cfg_base = @_;
    1 < scalar(@cfg_base)
      and croak "desktop_cfg_dir(;\$), not desktop_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
    $desktop_cfg_dir->(@cfg_base);
}

no warnings 'once';
*machine_cfg_dir = \&desktop_cfg_dir;
*xdg_config_dirs = \&desktop_cfg_dir;
use warnings;

=head2 core_cfg_dir

Returns the C<etc> directory below C<$Config{prefix}>.

=cut

my $core_cfg_dir = sub {
    my @cfg_base = @_;

lib/File/ConfigDir.pm  view on Meta::CPAN

sub user_cfg_dir
{
    my @cfg_base = @_;
    1 < scalar(@cfg_base)
      and croak "user_cfg_dir(;\$), not user_cfg_dir(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
    $user_cfg_dir->(@cfg_base);
}

=head2 xdg_config_home

Returns the user configuration directory for desktop applications.
If C<< $ENV{XDG_CONFIG_HOME} >> is not set, for MSWin32 the value
of C<< $ENV{APPDATA} >> is return and on Unices the C<.config> directory
in the users home folder. Without L<File::HomeDir>, on Unices the returned
list might be empty.

=cut

my $xdg_config_home = sub {
    my @cfg_base = @_;
    my @dirs;

lib/File/ConfigDir.pm  view on Meta::CPAN

sub xdg_config_home
{
    my @cfg_base = @_;
    1 < scalar(@cfg_base)
      and croak "xdg_config_home(;\$), not xdg_config_home(" . join(",", ("\$") x scalar(@cfg_base)) . ")";
    $xdg_config_home->(@cfg_base);
}

my (@extensible_bases, @pure_bases);
push(@extensible_bases,
    $system_cfg_dir, $desktop_cfg_dir, $local_cfg_dir,    $singleapp_cfg_dir, $vendorapp_cfg_dir, $core_cfg_dir,
    $site_cfg_dir,   $vendor_cfg_dir,  $locallib_cfg_dir, $here_cfg_dir,      $user_cfg_dir,      $xdg_config_home);
push(@pure_bases, 3);

=head2 config_dirs

    @cfgdirs = config_dirs();
    @cfgdirs = config_dirs( 'appname' );

Tries to get all available configuration directories as described above.
Returns those who exists and are readable.

t/01-simple.t  view on Meta::CPAN


my $dir = tempdir(CLEANUP => 1);

my $have_local_lib = eval "use local::lib '$dir'; 1;";

use Test::More;

use File::ConfigDir ':ALL';

my @supported_functions = (
    qw(config_dirs system_cfg_dir desktop_cfg_dir),
    qw(core_cfg_dir site_cfg_dir vendor_cfg_dir),
    qw(local_cfg_dir here_cfg_dir singleapp_cfg_dir vendorapp_cfg_dir),
    qw(xdg_config_dirs xdg_config_home user_cfg_dir locallib_cfg_dir),
);
foreach my $fn (@supported_functions)
{
    my $faddr;
    ok($faddr = File::ConfigDir->can($fn), "Can $fn");
    my @dirs = &{$faddr}();
    note("$fn: " . join(",", @dirs));
    if ($fn =~ m/(?:xdg_)?config_dirs/ or $fn =~ m/(?:machine|desktop)_cfg_dir/)
    {
        ok(scalar @dirs >= 1, $fn) or diag(join(",", @dirs));    # we expect at least system_cfg_dir
    }
    elsif ($fn eq "locallib_cfg_dir")
    {
        $have_local_lib and ok(scalar @dirs >= 1, $fn) or diag(join(",", @dirs));
        $have_local_lib or ok(0 == scalar @dirs, $fn) or diag(join(",", @dirs));
    }
    elsif ($fn =~ m/(?:local|user)_cfg_dir/ || $fn eq "xdg_config_home")
    {

t/05-errors.t  view on Meta::CPAN

#!perl

use strict;
use warnings;

use Test::More;

use File::ConfigDir ();

foreach my $fn (
    qw(config_dirs system_cfg_dir desktop_cfg_dir),
    qw(core_cfg_dir site_cfg_dir vendor_cfg_dir),
    qw(local_cfg_dir here_cfg_dir vendorapp_cfg_dir),
    qw(xdg_config_home user_cfg_dir locallib_cfg_dir),
  )
{
    my $faddr = File::ConfigDir->can($fn);
    eval { $faddr->(qw(foo bar)); };
    my $exception = $@;
    like($exception, qr/$fn\(;\$\), not $fn\(\$,\$\)/, "$fn throws exception on misuse");
}



( run in 0.552 second using v1.01-cache-2.11-cpan-299005ec8e3 )