blib/lib/File/XDG.pm

filter: Statementstmt 91.6% 55 / 60 Branchbran 50.0% 4 / 8 Conditioncond 75.0% 12 / 16 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 94.4% 17 / 18 Podpod 100% 8 / 8 totaltotal 84.8% 101 / 119 CC 12 SCAR 10.6
CC
12
Cov
85%
_home
6.2
_dirs
4.1
new
3.6
CRAP 2.9
« Build ↑ Summary
1 package File::XDG;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use feature qw(:5.10);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.04_001';
8  
9 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Path::Class qw(dir file);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 C<File::XDG> - Basic implementation of the XDG base directory specification
17  
18 =head1 SYNOPSIS
19  
20 use File::XDG;
21  
22 my $xdg = File::XDG->new(name => 'foo');
23  
24 # user config
25 $xdg->config_home
26  
27 # user data
28 $xdg->data_home
29  
30 # user cache
31 $xdg->cache_home
32  
33 # system config
34 $xdg->config_dirs
35  
36 # system data
37 $xdg->data_dirs
38  
39 =head1 DESCRIPTION
40  
41 This module provides a basic implementation of the XDG base directory
42 specification as exists by the Free Desktop Organization (FDO). It supports
43 all XDG directories except for the runtime directories, which require session
44 management support in order to function.
45  
46 =cut
47  
48 =head1 CONSTRUCTOR
49  
50 =cut
51  
52 =head2 $xdg = File::XDG->new( %args )
53  
54 Returns a new instance of a C<File::XDG> object. This must be called with an
55 application name as the C<name> argument.
56  
57 Takes the following named arguments:
58  
59 =over 8
60  
61 =item name => STRING
62  
63 Name of the application for which File::XDG is being used.
64  
65 =back
66  
67 =cut
68  
69 sub new {
70 4 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=12.7
Pod documented
71 4 my %args = (@_);
72  
73 my $self = {
74 4 ▶ name => delete $args{name} // croak('application name required'),
Condition33%1/3
delete $args{"name"} // croak("application name required")
l!l&&r!l&&!r
400
MC/DC0%0/2
delete $args{"name"} // croak("application name required")
delete $args{"name"} croak("application name required")
Truth table33%1/3
A // B
A= delete $args{"name"}B= croak("application name required")
ABresult
1X1
011
000
75 };
76  
77 4 ▶ return bless $self, $class || ref $class;
Condition33%1/3
$class || ref $class
l!l&&r!l&&!r
400
MC/DC0%0/2
$class || ref $class
$class ref $class
Truth table33%1/3
A || B
A= $classB= ref $class
ABresult
1X1
011
000
78 }
79  
80 sub _win {
81 0 ▶ my ($type) = @_;
Subroutine _win: not called CC=1 SCAR=6.9
82  
83 0 return File::HomeDir->my_data;
84 }
85  
86 sub _home {
87 14 ▶ my ($type) = @_;
Subroutine _home: called CC=6 SCAR=18.3
88 14 my $home = $ENV{HOME};
89  
90 14 ▶ $home = _win($type) if ($^O eq 'MSWin32');
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 14 14
91  
92 my %locations = (
93 data => ($ENV{XDG_DATA_HOME} || "$home/.local/share/"),
94 cache => ($ENV{XDG_CACHE_HOME} || "$home/.cache/"),
95 14 ▶ config => ($ENV{XDG_CONFIG_HOME} || "$home/.config/"),
Condition100%6/6
$ENV{'XDG_DATA_HOME'} || "$home/.local/share/"
l!l
113
$ENV{'XDG_CACHE_HOME'} || "$home/.cache/"
l!l
113
$ENV{'XDG_CONFIG_HOME'} || "$home/.config/"
l!l
113
MC/DC100%1/1
$ENV{'XDG_DATA_HOME'} || "$home/.local/share/"
$ENV{'XDG_DATA_HOME'}
MC/DC100%1/1
$ENV{'XDG_CACHE_HOME'} || "$home/.cache/"
$ENV{'XDG_CACHE_HOME'}
MC/DC100%1/1
$ENV{'XDG_CONFIG_HOME'} || "$home/.config/"
$ENV{'XDG_CONFIG_HOME'}
Truth table100%2/2
A
A= $ENV{'XDG_DATA_HOME'}
Aresult
11
00
Truth table100%2/2
A
A= $ENV{'XDG_CACHE_HOME'}
Aresult
11
00
Truth table100%2/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
96 );
97  
98 14 ▶ return $locations{$type} if exists $locations{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $locations{$type} 14 0 14
99 0 croak 'invalid _home requested';
100 }
101  
102 sub _dirs {
103 10 ▶ my $type = shift;
Subroutine _dirs: called CC=4 SCAR=14.2
104  
105 my %locations = (
106 data => ($ENV{XDG_DATA_DIRS} || '/usr/local/share:/usr/share'),
107 10 ▶ config => ($ENV{XDG_CONFIG_DIRS} || '/etc/xdg'),
Condition100%4/4
$ENV{'XDG_DATA_DIRS'} || '/usr/local/share:/usr/share'
l!l
91
$ENV{'XDG_CONFIG_DIRS'} || '/etc/xdg'
l!l
91
MC/DC100%1/1
$ENV{'XDG_DATA_DIRS'} || '/usr/local/share:/usr/share'
$ENV{'XDG_DATA_DIRS'}
MC/DC100%1/1
$ENV{'XDG_CONFIG_DIRS'} || '/etc/xdg'
$ENV{'XDG_CONFIG_DIRS'}
Truth table100%2/2
A
A= $ENV{'XDG_DATA_DIRS'}
Aresult
11
00
Truth table100%2/2
A
A= $ENV{'XDG_CONFIG_DIRS'}
Aresult
11
00
108 );
109  
110 10 ▶ return $locations{$type} if exists $locations{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $locations{$type} 10 0 10
111 0 croak 'invalid _dirs requested';
112 }
113  
114 sub _lookup_file {
115 6 ▶ my ($self, $type, @subpath) = @_;
Subroutine _lookup_file: called CC=2 SCAR=7.1
116  
117 6 ▶ unless (@subpath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@subpath) 0 6 6
118 0 croak 'subpath not specified';
119 }
120  
121 6 my @dirs = (_home($type), split(':', _dirs($type)));
122 6 my @paths = map { file($_, @subpath) } @dirs;
123 6 my ($match) = grep { -f $_ } @paths;
124  
125 6 return $match;
126 }
127  
128 =head1 METHODS
129  
130 =cut
131  
132 =head2 $xdg->data_home()
133  
134 Returns the user-specific data directory for the application as a C<Path::Class> object.
135  
136 =cut
137  
138 sub data_home {
139 3 ▶ my $self = shift;
Subroutine data_home: called CC=1 SCAR=0.0
Pod documented
140 3 my $xdg = _home('data');
141 3 return dir($xdg, $self->{name});
142 }
143  
144 =head2 $xdg->config_home()
145  
146 Returns the user-specific configuration directory for the application as a C<Path::Class> object.
147  
148 =cut
149  
150 sub config_home {
151 3 ▶ my $self = shift;
Subroutine config_home: called CC=1 SCAR=0.0
Pod documented
152 3 my $xdg = _home('config');
153 3 return dir($xdg, $self->{name});
154 }
155  
156 =head2 $xdg->cache_home()
157  
158 Returns the user-specific cache directory for the application as a C<Path::Class> object.
159  
160 =cut
161  
162 sub cache_home {
163 2 ▶ my $self = shift;
Subroutine cache_home: called CC=1 SCAR=0.0
Pod documented
164 2 my $xdg = _home('cache');
165 2 return dir($xdg, $self->{name});
166 }
167  
168 =head2 $xdg->data_dirs()
169  
170 Returns the system data directories, not modified for the application. Per the
171 specification, the returned string is :-delimited.
172  
173 =cut
174  
175 sub data_dirs {
176 2 ▶ return _dirs('data');
Subroutine data_dirs: called CC=1 SCAR=0.0
Pod documented
177 }
178  
179 =head2 $xdg->config_dirs()
180  
181 Returns the system config directories, not modified for the application. Per
182 the specification, the returned string is :-delimited.
183  
184 =cut
185  
186 sub config_dirs {
187 2 ▶ return _dirs('config');
Subroutine config_dirs: called CC=1 SCAR=0.0
Pod documented
188 }
189  
190 =head2 $xdg->lookup_data_file('subdir', 'filename');
191  
192 Looks up the data file by searching for ./subdir/filename relative to all base
193 directories indicated by $XDG_DATA_HOME and $XDG_DATA_DIRS. If an environment
194 variable is either not set or empty, its default value as defined by the
195 specification is used instead. Returns a C<Path::Class> object.
196  
197 =cut
198  
199 sub lookup_data_file {
200 3 ▶ my ($self, @subpath) = @_;
Subroutine lookup_data_file: called CC=1 SCAR=0.0
Pod documented
201 3 return $self->_lookup_file('data', @subpath);
202 }
203  
204 =head2 $xdg->lookup_config_file('subdir', 'filename');
205  
206 Looks up the configuration file by searching for ./subdir/filename relative to
207 all base directories indicated by $XDG_CONFIG_HOME and $XDG_CONFIG_DIRS. If an
208 environment variable is either not set or empty, its default value as defined
209 by the specification is used instead. Returns a C<Path::Class> object.
210  
211 =cut
212  
213 sub lookup_config_file {
214 3 ▶ my ($self, @subpath) = @_;
Subroutine lookup_config_file: called CC=1 SCAR=0.0
Pod documented
215 3 return $self->_lookup_file('config', @subpath);
216 }
217  
218 =head1 SEE ALSO
219  
220 L<XDG Base Directory specification, version 0.7|http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>
221  
222 =head1 ACKNOWLEDGEMENTS
223  
224 This module's Windows support is made possible by C<File::HomeDir>. I would also like to thank C<Path::Class> and C<File::Spec>.
225  
226 =head1 AUTHOR
227  
228 Kiyoshi Aman <kiyoshi.aman@gmail.com>
229  
230 =cut
231  
232 4 1;
« Build ↑ Summary