1File::HomeDir(3) User Contributed Perl Documentation File::HomeDir(3)
2
3
4
6 File::HomeDir - Find your home and other directories on any platform
7
9 use File::HomeDir;
10
11 # Modern Interface (Current User)
12 $home = File::HomeDir->my_home;
13 $desktop = File::HomeDir->my_desktop;
14 $docs = File::HomeDir->my_documents;
15 $music = File::HomeDir->my_music;
16 $pics = File::HomeDir->my_pictures;
17 $videos = File::HomeDir->my_videos;
18 $data = File::HomeDir->my_data;
19 $dist = File::HomeDir->my_dist_data('File-HomeDir');
20 $dist = File::HomeDir->my_dist_config('File-HomeDir');
21
22 # Modern Interface (Other Users)
23 $home = File::HomeDir->users_home('foo');
24 $desktop = File::HomeDir->users_desktop('foo');
25 $docs = File::HomeDir->users_documents('foo');
26 $music = File::HomeDir->users_music('foo');
27 $pics = File::HomeDir->users_pictures('foo');
28 $video = File::HomeDir->users_videos('foo');
29 $data = File::HomeDir->users_data('foo');
30
32 File::HomeDir is a module for locating the directories that are "owned"
33 by a user (typicaly your user) and to solve the various issues that
34 arise trying to find them consistently across a wide variety of
35 platforms.
36
37 The end result is a single API that can find your resources on any
38 platform, making it relatively trivial to create Perl software that
39 works elegantly and correctly no matter where you run it.
40
41 This module provides two main interfaces.
42
43 The first is a modern File::Spec-style interface with a consistent OO
44 API and different implementation modules to support various platforms.
45 You are strongly recommended to use this interface.
46
47 The second interface is for legacy support of the original 0.07
48 interface that exported a "home()" function by default and tied the
49 "%~" variable.
50
51 It is generally not recommended that you use this interface, but due to
52 back-compatibility reasons they will remain supported until at least
53 2010.
54
55 The "%~" interface has been deprecated. Documentation was removed in
56 2009, Unit test were removed in 2011, usage will issue warnings from
57 2012, and the interface will be removed entirely in 2015 (in line with
58 the general Perl toolchain convention of a 10 year support period for
59 legacy APIs that are potentially or actually in common use).
60
61 Platform Neutrality
62 In the Unix world, many different types of data can be mixed together
63 in your home directory (although on some Unix platforms this is no
64 longer the case, particularly for "desktop"-oriented platforms).
65
66 On some non-Unix platforms, separate directories are allocated for
67 different types of data and have been for a long time.
68
69 When writing applications on top of File::HomeDir, you should thus
70 always try to use the most specific method you can. User documents
71 should be saved in "my_documents", data that supports an application
72 but isn't normally editing by the user directory should go into
73 "my_data".
74
75 On platforms that do not make any distinction, all these different
76 methods will harmlessly degrade to the main home directory, but on
77 platforms that care File::HomeDir will always try to Do The Right
78 Thing(tm).
79
81 Two types of methods are provided. The "my_method" series of methods
82 for finding resources for the current user, and the "users_method"
83 (read as "user's method") series for finding resources for arbitrary
84 users.
85
86 This split is necessary, as on most platforms it is much easier to find
87 information about the current user compared to other users, and indeed
88 on a number you cannot find out information such as "users_desktop" at
89 all, due to security restrictions.
90
91 All methods will double check (using a "-d" test) that a directory
92 actually exists before returning it, so you may trust in the values
93 that are returned (subject to the usual caveats of race conditions of
94 directories being deleted at the moment between a directory being
95 returned and you using it).
96
97 However, because in some cases platforms may not support the concept of
98 home directories at all, any method may return "undef" (both in scalar
99 and list context) to indicate that there is no matching directory on
100 the system.
101
102 For example, most untrusted 'nobody'-type users do not have a home
103 directory. So any modules that are used in a CGI application that at
104 some level of recursion use your code, will result in calls to
105 File::HomeDir returning undef, even for a basic home() call.
106
107 my_home
108 The "my_home" method takes no arguments and returns the main
109 home/profile directory for the current user.
110
111 If the distinction is important to you, the term "current" refers to
112 the real user, and not the effective user.
113
114 This is also the case for all of the other "my" methods.
115
116 Returns the directory path as a string, "undef" if the current user
117 does not have a home directory, or dies on error.
118
119 my_desktop
120 The "my_desktop" method takes no arguments and returns the "desktop"
121 directory for the current user.
122
123 Due to the diversity and complexity of implementions required to deal
124 with implementing the required functionality fully and completely, the
125 "my_desktop" method may or may not be implemented on each platform.
126
127 That said, I am extremely interested in code to implement "my_desktop"
128 on Unix, as long as it is capable of dealing (as the Windows
129 implementation does) with internationalisation. It should also avoid
130 false positive results by making sure it only returns the appropriate
131 directories for the appropriate platforms.
132
133 Returns the directory path as a string, "undef" if the current user
134 does not have a desktop directory, or dies on error.
135
136 my_documents
137 The "my_documents" method takes no arguments and returns the directory
138 (for the current user) where the user's documents are stored.
139
140 Returns the directory path as a string, "undef" if the current user
141 does not have a documents directory, or dies on error.
142
143 my_music
144 The "my_music" method takes no arguments and returns the directory
145 where the current user's music is stored.
146
147 No bias is made to any particular music type or music program, rather
148 the concept of a directory to hold the user's music is made at the
149 level of the underlying operating system or (at least) desktop
150 environment.
151
152 Returns the directory path as a string, "undef" if the current user
153 does not have a suitable directory, or dies on error.
154
155 my_pictures
156 The "my_pictures" method takes no arguments and returns the directory
157 where the current user's pictures are stored.
158
159 No bias is made to any particular picture type or picture program,
160 rather the concept of a directory to hold the user's pictures is made
161 at the level of the underlying operating system or (at least) desktop
162 environment.
163
164 Returns the directory path as a string, "undef" if the current user
165 does not have a suitable directory, or dies on error.
166
167 my_videos
168 The "my_videos" method takes no arguments and returns the directory
169 where the current user's videos are stored.
170
171 No bias is made to any particular video type or video program, rather
172 the concept of a directory to hold the user's videos is made at the
173 level of the underlying operating system or (at least) desktop
174 environment.
175
176 Returns the directory path as a string, "undef" if the current user
177 does not have a suitable directory, or dies on error.
178
179 my_data
180 The "my_data" method takes no arguments and returns the directory where
181 local applications should stored their internal data for the current
182 user.
183
184 Generally an application would create a subdirectory such as ".foo",
185 beneath this directory, and store its data there. By creating your
186 directory this way, you get an accurate result on the maximum number of
187 platforms. But see the documentation about "my_dist_config()" or
188 "my_dist_data()" below.
189
190 For example, on Unix you get "~/.foo" and on Win32 you get "~/Local
191 Settings/Application Data/.foo"
192
193 Returns the directory path as a string, "undef" if the current user
194 does not have a data directory, or dies on error.
195
196 my_dist_config
197 File::HomeDir->my_dist_config( $dist [, \%params] );
198
199 # For example...
200
201 File::HomeDir->my_dist_config( 'File-HomeDir' );
202 File::HomeDir->my_dist_config( 'File-HomeDir', { create => 1 } );
203
204 The "my_dist_config" method takes a distribution name as argument and
205 returns an application-specific directory where they should store their
206 internal configuration.
207
208 The base directory will be either "my_config" if the platform supports
209 it, or "my_documents" otherwise. The subdirectory itself will be
210 "BASE/Perl/Dist-Name". If the base directory is the user's homedir,
211 "my_dist_config" will be in "~/.perl/Dist-Name" (and thus be hidden on
212 all Unixes).
213
214 The optional last argument is a hash reference to tweak the method
215 behaviour. The following hash keys are recognized:
216
217 · create
218
219 Passing a true value to this key will force the creation of the
220 directory if it doesn't exist (remember that "File::HomeDir"'s
221 policy is to return "undef" if the directory doesn't exist).
222
223 Defaults to false, meaning no automatic creation of directory.
224
225 my_dist_data
226 File::HomeDir->my_dist_data( $dist [, \%params] );
227
228 # For example...
229
230 File::HomeDir->my_dist_data( 'File-HomeDir' );
231 File::HomeDir->my_dist_data( 'File-HomeDir', { create => 1 } );
232
233 The "my_dist_data" method takes a distribution name as argument and
234 returns an application-specific directory where they should store their
235 internal data.
236
237 This directory will be of course a subdirectory of "my_data". Platforms
238 supporting data-specific directories will use
239 "DATA_DIR/perl/dist/Dist-Name" following the common
240 "DATA/vendor/application" pattern. If the "my_data" directory is the
241 user's homedir, "my_dist_data" will be in "~/.perl/dist/Dist-Name" (and
242 thus be hidden on all Unixes).
243
244 The optional last argument is a hash reference to tweak the method
245 behaviour. The following hash keys are recognized:
246
247 · create
248
249 Passing a true value to this key will force the creation of the
250 directory if it doesn't exist (remember that "File::HomeDir"'s
251 policy is to return "undef" if the directory doesn't exist).
252
253 Defaults to false, meaning no automatic creation of directory.
254
255 users_home
256 $home = File::HomeDir->users_home('foo');
257
258 The "users_home" method takes a single param and is used to locate the
259 parent home/profile directory for an identified user on the system.
260
261 While most of the time this identifier would be some form of user name,
262 it is permitted to vary per-platform to support user ids or UUIDs as
263 applicable for that platform.
264
265 Returns the directory path as a string, "undef" if that user does not
266 have a home directory, or dies on error.
267
268 users_documents
269 $docs = File::HomeDir->users_documents('foo');
270
271 Returns the directory path as a string, "undef" if that user does not
272 have a documents directory, or dies on error.
273
274 users_data
275 $data = File::HomeDir->users_data('foo');
276
277 Returns the directory path as a string, "undef" if that user does not
278 have a data directory, or dies on error.
279
281 home
282 use File::HomeDir;
283 $home = home();
284 $home = home('foo');
285 $home = File::HomeDir::home();
286 $home = File::HomeDir::home('foo');
287
288 The "home" function is exported by default and is provided for
289 compatibility with legacy applications. In new applications, you should
290 use the newer method-based interface above.
291
292 Returns the directory path to a named user's home/profile directory.
293
294 If provided no param, returns the directory path to the current user's
295 home/profile directory.
296
298 · Add more granularity to Unix, and add support to VMS and other
299 esoteric platforms, so we can consider going core.
300
301 · Add consistent support for users_* methods
302
304 This module is stored in an Open Repository at the following address.
305
306 <http://svn.ali.as/cpan/trunk/File-HomeDir>
307
308 Write access to the repository is made available automatically to any
309 published CPAN author, and to most other volunteers on request.
310
311 If you are able to submit your bug report in the form of new (failing)
312 unit tests, or can apply your fix directly instead of submitting a
313 patch, you are strongly encouraged to do so as the author currently
314 maintains over 100 modules and it can take some time to deal with non-
315 Critical bug reports or patches.
316
317 This will guarantee that your issue will be addressed in the next
318 release of the module.
319
320 If you cannot provide a direct test or fix, or don't have time to do
321 so, then regular bug reports are still accepted and appreciated via the
322 CPAN bug tracker.
323
324 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-HomeDir>
325
326 For other issues, for commercial enhancement or support, or to have
327 your write access enabled for the repository, contact the author at the
328 email address above.
329
331 The biggest acknowledgement goes to Chris Nandor, who wielded his
332 legendary Mac-fu and turned my initial fairly ordinary Darwin
333 implementation into something that actually worked properly everywhere,
334 and then donated a Mac OS X license to allow it to be maintained
335 properly.
336
338 Adam Kennedy <adamk@cpan.org>
339
340 Sean M. Burke <sburke@cpan.org>
341
342 Chris Nandor <cnandor@cpan.org>
343
344 Stephen Steneker <stennie@cpan.org>
345
347 File::ShareDir, File::HomeDir::Win32 (legacy)
348
350 Copyright 2005 - 2012 Adam Kennedy.
351
352 Some parts copyright 2000 Sean M. Burke.
353
354 Some parts copyright 2006 Chris Nandor.
355
356 Some parts copyright 2006 Stephen Steneker.
357
358 Some parts copyright 2009-2011 Jerome Quelin.
359
360 This program is free software; you can redistribute it and/or modify it
361 under the same terms as Perl itself.
362
363 The full text of the license can be found in the LICENSE file included
364 with this module.
365
367 Hey! The above document had some coding errors, which are explained
368 below:
369
370 Around line 712:
371 Non-ASCII character seen before =encoding in 'Jerome'. Assuming
372 UTF-8
373
374
375
376perl v5.16.3 2012-10-19 File::HomeDir(3)