📄 homedir.pm
字号:
2015 (maintaining the general Perl convention of a 10 year support periodfor legacy APIs potentially or actually in common use).=head2 Platform NeutralityIn the Unix world, many different types of data can be mixed togetherin your home directory (although on some Unix platforms this is no longerthe case, particularly for "desktop"-oriented platforms).On some non-Unix platforms, seperate directories are allocated fordifferent types of data and have been for a long time.When writing applications on top of B<File::HomeDir>, you should thusalways try to use the most specific method you can. User documents shouldbe saved in C<my_documents>, data that supports an application but isn'tnormally editing by the user directory should go into C<my_data>.On platforms that do not make any distinction, all these differentmethods will harmlessly degrade to the main home directory, but onplatforms that care B<File::HomeDir> will always try to Do The RightThing(tm).=head1 METHODSTwo types of methods are provided. The C<my_method> series of methods forfinding resources for the current user, and the C<users_method> (read as"user's method") series for finding resources for arbitrary users.This split is necesary, as on most platforms it is B<much> easier to findinformation about the current user compared to other users, and indeedon a number you cannot find out information such as C<users_desktop> atall, due to security restrictions.All methods will double check (using a C<-d> test) that a directoryactually exists before returning it, so you may trust in the valuesthat are returned (subject to the usual caveats of race conditions ofdirectories being deleted at the moment between a directory being returnedand you using it).However, because in some cases platforms may not support the concept of homedirectories at all, any method may return C<undef> (both in scalar and listcontext) to indicate that there is no matching directory on the system.For example, most untrusted 'nobody'-type users do not have a homedirectory. So any modules that are used in a CGI application thatat some level of recursion use your code, will result in calls toFile::HomeDir returning undef, even for a basic home() call.=head2 my_homeThe C<my_home> method takes no arguments and returns the main home/profiledirectory for the current user.If the distinction is important to you, the term "current" refers to thereal user, and not the effective user.This is also the case for all of the other "my" methods.Returns the directory path as a string, C<undef> if the current userdoes not have a home directory, or dies on error.=head2 my_desktopThe C<my_desktop> method takes no arguments and returns the "desktop"directory for the current user.Due to the diversity and complexity of implementions required to deal withimplementing the required functionality fully and completely, for the momentC<my_desktop> is B<not> going to be implemented.That said, I am extremely interested in code to implement C<my_desktop> onUnix, as long as it is capable of dealing (as the Windows implementationdoes) with internationalisation. It should also avoid false positiveresults by making sure it only returns the appropriate directories for theappropriate platforms.Returns the directory path as a string, C<undef> if the current userdoes not have a desktop directory, or dies on error.=head2 my_documentsThe C<my_documents> method takes no arguments and returns the directory (forthe current user) where the user's documents are stored.Returns the directory path as a string, C<undef> if the current userdoes not have a documents directory, or dies on error.=head2 my_musicThe C<my_music> method takes no arguments and returns the directorywhere the current user's music is stored.No bias is made to any particular music type or music program, rather theconcept of a directory to hold the user's music is made at the level of theunderlying operating system or (at least) desktop environment.Returns the directory path as a string, C<undef> if the current userdoes not have a suitable directory, or dies on error.=head2 my_picturesThe C<my_pictures> method takes no arguments and returns the directorywhere the current user's pictures are stored.No bias is made to any particular picture type or picture program, rather theconcept of a directory to hold the user's pictures is made at the level of theunderlying operating system or (at least) desktop environment.Returns the directory path as a string, C<undef> if the current userdoes not have a suitable directory, or dies on error.=head2 my_videosThe C<my_videos> method takes no arguments and returns the directorywhere the current user's videos are stored.No bias is made to any particular video type or video program, rather theconcept of a directory to hold the user's videos is made at the level of theunderlying operating system or (at least) desktop environment.Returns the directory path as a string, C<undef> if the current userdoes not have a suitable directory, or dies on error.=head2 my_dataThe C<my_data> takes no arguments and returns the directory wherelocal applications should stored their internal data for the currentuser.Generally an application would create a subdirectory such as C<.foo>,beneath this directory, and store its data there. By creating yourdirectory this way, you get an accurate result on the maximum numberof platforms.For example, on Unix you get C<~/.foo> and on Win32 you getC<~/Local Settings/Application Data/.foo>Returns the directory path as a string, C<undef> if the current userdoes not have a data directory, or dies on error.=head2 users_home $home = File::HomeDir->users_home('foo');The C<users_home> method takes a single param and is used to locate theparent home/profile directory for an identified user on the system.While most of the time this identifier would be some form of user name,it is permitted to vary per-platform to support user ids or UUIDs asapplicable for that platform.Returns the directory path as a string, C<undef> if that userdoes not have a home directory, or dies on error.=head2 users_documents $docs = File::HomeDir->users_documents('foo');Returns the directory path as a string, C<undef> if that userdoes not have a documents directory, or dies on error.=head2 users_data $data = File::HomeDir->users_data('foo');Returns the directory path as a string, C<undef> if that userdoes not have a data directory, or dies on error.=head1 FUNCTIONS=head2 home use File::HomeDir; $home = home(); $home = home('foo'); $home = File::HomeDir::home(); $home = File::HomeDir::home('foo');The C<home> function is exported by default and is provided forcompatibility with legacy applications. In new applications, you shoulduse the newer method-based interface above.Returns the directory path to a named user's home/profile directory.If provided no param, returns the directory path to the current user'shome/profile directory.=head1 TIED INTERFACE=head2 %~ $home = $~{""}; $home = $~{undef}; $home = $~{$user}; $home = $~{username}; print "... $~{''} ..."; print "... $~{$user} ..."; print "... $~{username} ...";This calls C<home($user)> or C<home('username')> -- except that if youask for C<$~{some_user}> and there is no such user, it will die.Note that this is especially useful in double-quotish strings, like: print "Jojo's .newsrc is ", -s "$~{jojo}/.newsrc", "b long!\n"; # (helpfully dies if there is no user 'jojo')If you want to avoid the fatal errors, first test the value ofC<home('jojo')>, which will return undef (instead of dying) in case ofthere being no such user.Note, however, that if the hash key is "" or undef (whether thru beinga literal "", or a scalar whose value is empty-string or undef), thenthis returns zero-argument C<home()>, i.e., your home directory:Further, please note that because the %~ hash compulsorily modifiesa hash outside of it's namespace, and presents an overly simplisticapproach to home directories, it is likely to ultimately be removed.The interface is currently expected to be formally deprecated from 2010(but no earlier) and removed from 2015 (but no earlier). If very heavyuse is found in the wild, these plans may be pushed back.=head1 TO DO=over 4=item * Become generally clearer on situations in which a user might nothave a particular resource.=item * Merge remaining edge case code in File::HomeDir::Win32=item * Add more granularity to Unix, and add support to VMS and otheresoteric platforms, so we can consider going core.=item * Add consistent support for users_* methods =back=head1 SUPPORTThis module is stored in an Open Repository at the following address.L<http://svn.ali.as/cpan/trunk/File-HomeDir>Write access to the repository is made available automatically to anypublished CPAN author, and to most other volunteers on request.If you are able to submit your bug report in the form of new (failing)unit tests, or can apply your fix directly instead of submitting a patch,you are B<strongly> encouraged to do so as the author currently maintainsover 100 modules and it can take some time to deal with non-Critical bugreports or patches.This will guarantee that your issue will be addressed in the nextrelease of the module.If you cannot provide a direct test or fix, or don't have time to do so,then regular bug reports are still accepted and appreciated via the CPANbug tracker.L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-HomeDir>For other issues, for commercial enhancement or support, or to have yourwrite access enabled for the repository, contact the author at the emailaddress above.=head1 ACKNOWLEDGEMENTSThe biggest acknowledgement must go to Chris Nandor, who wielded hislegendary Mac-fu and turned my initial fairly ordinary Darwinimplementation into something that actually worked properly everywhere,and then donated a Mac OS X license to allow it to be maintained properly.=head1 AUTHORSAdam Kennedy E<lt>adamk@cpan.orgE<gt>Sean M. Burke E<lt>sburke@cpan.orgE<gt>Chris Nandor E<lt>cnandor@cpan.orgE<gt>Stephen Steneker E<lt>stennie@cpan.orgE<gt>=head1 SEE ALSOL<File::ShareDir>, L<File::HomeDir::Win32> (legacy)=head1 COPYRIGHTCopyright 2005, 2006 Adam Kennedy.Some parts copyright 2000 Sean M. Burke.Some parts copyright 2006 Chris Nandor.Some parts copyright 2006 Stephen Steneker.This program is free software; you can redistributeit and/or modify it under the same terms as Perl itself.The full text of the license can be found in theLICENSE file included with this module.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -