File::HomeDir(3)

๐Ÿ’– File::HomeDir(3) ๐Ÿ’–

User Contributed Perl Documentation, bestie โ€” welcome to the manual page for File::HomeDir. It's literally your map to finding homes and other directories on any platform. ๐Ÿ โœจ


๐ŸŒธ NAME ๐ŸŒธ

File::HomeDir โ€” find your home and other directories on any platform. Iconic behavior. ๐Ÿ‘‘


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

Okay so here's the whole vibe in code form โ€” copy-paste with confidence, bestie:

  use File::HomeDir;

  # Modern Interface (Current User)
  $home     = File::HomeDir->my_home;
  $desktop  = File::HomeDir->my_desktop;
  $docs     = File::HomeDir->my_documents;
  $music    = File::HomeDir->my_music;
  $pics     = File::HomeDir->my_pictures;
  $videos   = File::HomeDir->my_videos;
  $data     = File::HomeDir->my_data;
  $dist     = File::HomeDir->my_dist_data('File-HomeDir');
  $dist     = File::HomeDir->my_dist_config('File-HomeDir');

  # Modern Interface (Other Users)
  $home    = File::HomeDir->users_home('foo');
  $desktop = File::HomeDir->users_desktop('foo');
  $docs    = File::HomeDir->users_documents('foo');
  $music   = File::HomeDir->users_music('foo');
  $pics    = File::HomeDir->users_pictures('foo');
  $video   = File::HomeDir->users_videos('foo');
  $data    = File::HomeDir->users_data('foo');

The way this synopsis ate and left no crumbs? Obsessed. ๐Ÿ’…


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So File::HomeDir is this module for locating the directories that are "owned" by a user โ€” usually your user โ€” and for solving all the little issues that pop up when you're trying to find those directories consistently across a whole bunch of different platforms. ๐Ÿ˜ญ๐Ÿ’ป

The big payoff is that you get a single API that can find your resources on any platform. That makes it relatively trivial to build Perl software that works elegantly and correctly no matter where you run it. No cap, that's the dream. โœจ

๐ŸŒˆ Platform Neutrality ๐ŸŒˆ

In the Unix world, lots of different types of data can get all mixed together in your home directory. (Although on some Unix platforms this is no longer the case โ€” especially on "desktop"-oriented platforms.) ๐Ÿ—‚๏ธ๐ŸŒธ

On some non-Unix platforms, separate directories are allocated for different types of data, and they've been doing it that way for a long time. Different platform, different vibe. ๐Ÿฆ‹

So when you're writing applications on top of File::HomeDir, you should always try to use the most specific method you can. Bestie, listen: user documents should be saved in my_documents, and data that supports an application but isn't normally edited by the user should go into my_data. ๐Ÿ“๐Ÿ’–

On platforms that don't make any distinction, all these different methods will harmlessly degrade to the main home directory. But on platforms that care? File::HomeDir will always try to Do The Right Thing(tm). ๐Ÿ‘‘๐Ÿ”ฅ

๐Ÿ”ฅ METHODS ๐Ÿ”ฅ

ok so there's two flavors of methods here, bestie, and you gotta know which one you're grabbing:

  • the my_method series ๐Ÿ’– โ€” finds stuff for the current user
  • the users_method series (read it out loud as "user's method") ๐ŸŒธ โ€” finds stuff for arbitrary users

Why the split? Because on most platforms it is way easier to find info about the current user than about other users โ€” and on a number of platforms you literally cannot find out things like users_desktop at all, thanks to security restrictions. So yeah, the split is necessary, not just vibes. ๐Ÿซ 

Every single method will double check (using a -d test) that a directory actually exists before returning it, so you can trust the values you get back โ€” subject to the usual caveats of race conditions of directories being deleted at the moment between a directory being returned and you using it. ๐Ÿ˜ญ classic.

But listen: because in some cases platforms may not support the concept of home directories at all, any method may return undef (both in scalar and list context) to signal that there's no matching directory on the system. Not an error, just... no directory. โœจ

Real example so it lands: most untrusted 'nobody'-type users do not have a home directory. So any modules used in a CGI application that at some level of recursion use your code will result in calls to File::HomeDir returning undef โ€” even for a basic home() call. ๐Ÿ’€ keep that in mind before you go blaming yourself.


๐Ÿ  my_home

The my_home method takes no arguments and returns the main home/profile directory for the current user.

If that distinction matters to you (and it might ๐Ÿ‘€): "current" refers to the real user, not the effective user. And yes, that's true for all the other my_ methods too.

Returns the directory path as a string, undef if the current user does not have a home directory, or dies on error.

๐Ÿ–ฅ๏ธ my_desktop

The my_desktop method takes no arguments and returns the "desktop" directory for the current user.

Now, due to the diversity and complexity of implementations required to deal with implementing the required functionality fully and completely (say that five times fast ๐Ÿ˜ฎโ€๐Ÿ’จ), the my_desktop method may or may not be implemented on each platform. It's giving "we tried our best."

That said, the author is extremely interested in code to implement my_desktop on Unix โ€” as long as it is capable of dealing (as the Windows implementation does) with internationalization. It should also avoid false positive results by making sure it only returns the appropriate directories for the appropriate platforms. ๐Ÿ“Œ

Returns the directory path as a string, undef if the current user does not have a desktop directory, or dies on error.

๐Ÿ“„ my_documents

The my_documents method takes no arguments and returns the directory (for the current user) where the user's documents are stored.

Returns the directory path as a string, undef if the current user does not have a documents directory, or dies on error.

๐ŸŽต my_music

The my_music method takes no arguments and returns the directory where the current user's music is stored.

No bias is made to any particular music type or music program ๐ŸŽง โ€” rather, the concept of a directory to hold the user's music is made at the level of the underlying operating system or (at least) desktop environment.

Returns the directory path as a string, undef if the current user does not have a suitable directory, or dies on error.

๐Ÿ–ผ๏ธ my_pictures

The my_pictures method takes no arguments and returns the directory where the current user's pictures are stored.

No bias is made to any particular picture type or picture program ๐Ÿ“ธ โ€” the concept of a directory to hold the user's pictures is made at the level of the underlying operating system or (at least) desktop environment.

Returns the directory path as a string, undef if the current user does not have a suitable directory, or dies on error.

๐ŸŽฌ my_videos

The my_videos method takes no arguments and returns the directory where the current user's videos are stored.

No bias is made to any particular video type or video program ๐Ÿฟ โ€” the concept of a directory to hold the user's videos is made at the level of the underlying operating system or (at least) desktop environment.

Returns the directory path as a string, undef if the current user does not have a suitable directory, or dies on error.

๐Ÿ’พ my_data

The my_data method takes no arguments and returns the directory where local applications should store their internal data for the current user.

Generally an application would create a subdirectory such as .foo, beneath this directory, and store its data there. By creating your directory this way, you get an accurate result on the maximum number of platforms. ๐Ÿ† But do peep the documentation about my_dist_config() or my_dist_data() below, fr.

For example, on Unix you get ~/.foo and on Win32 you get ~/Local Settings/Application Data/.foo

Returns the directory path as a string, undef if the current user does not have a data directory, or dies on error.

โš™๏ธ my_dist_config

File::HomeDir->my_dist_config( $dist [, \%params] );

# For example...

File::HomeDir->my_dist_config( 'File-HomeDir' );
File::HomeDir->my_dist_config( 'File-HomeDir', { create => 1 } );

The my_dist_config method takes a distribution name as argument and returns an application-specific directory where they should store their internal configuration. ๐Ÿ—‚๏ธ

The base directory will be either my_config if the platform supports it, or my_documents otherwise. The subdirectory itself will be BASE/Perl/Dist-Name. If the base directory is the user's home directory, my_dist_config will be in ~/.perl/Dist-Name (and thus be hidden on all Unixes ๐Ÿ™ˆ).

The optional last argument is a hash reference to tweak the method behaviour. These hash keys are recognized:

  • create โ€” passing a true value to this key will force the creation of the directory if it doesn't exist (remember that File::HomeDir's policy is to return undef if the directory doesn't exist). Defaults to false, meaning no automatic creation of directory.

๐Ÿ“ฆ my_dist_data

File::HomeDir->my_dist_data( $dist [, \%params] );

# For example...

File::HomeDir->my_dist_data( 'File-HomeDir' );
File::HomeDir->my_dist_data( 'File-HomeDir', { create => 1 } );

The my_dist_data method takes a distribution name as argument and returns an application-specific directory where they should store their internal data. ๐Ÿ’ฝ

This directory will be of course a subdirectory of my_data. Platforms supporting data-specific directories will use DATA_DIR/perl/dist/Dist-Name following the common DATA/vendor/application pattern. If the my_data directory is the user's home directory, my_dist_data will be in ~/.perl/dist/Dist-Name (and thus be hidden on all Unixes ๐Ÿ™ˆ).

The optional last argument is a hash reference to tweak the method behaviour. These hash keys are recognized:

  • create โ€” passing a true value to this key will force the creation of the directory if it doesn't exist (remember that File::HomeDir's policy is to return undef if the directory doesn't exist). Defaults to false, meaning no automatic creation of directory.

๐Ÿ‘ฅ users_home

$home = File::HomeDir->users_home('foo');

The users_home method takes a single parameter and is used to locate the parent 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 as applicable for that platform. ๐Ÿ†”

Returns the directory path as a string, undef if that user does not have a home directory, or dies on error.

๐Ÿ“‘ users_documents

$docs = File::HomeDir->users_documents('foo');

Returns the directory path as a string, undef if that user does not have a documents directory, or dies on error.

๐Ÿ’ฝ users_data

$data = File::HomeDir->users_data('foo');

Returns the directory path as a string, undef if that user does not have a data directory, or dies on error.

๐Ÿ–ฅ๏ธ users_desktop

$docs = File::HomeDir->users_desktop('foo');

Returns the directory path as a string, undef if that user does not have a desktop directory, or dies on error.

๐ŸŽถ users_music

$docs = File::HomeDir->users_music('foo');

Returns the directory path as a string, undef if that user does not have a music directory, or dies on error.

๐ŸŒท users_pictures

$docs = File::HomeDir->users_pictures('foo');

Returns the directory path as a string, undef if that user does not have a pictures directory, or dies on error.

๐ŸŽฅ users_videos

$docs = File::HomeDir->users_videos('foo');

Returns the directory path as a string, undef if that user does not have a videos directory, or dies on error.


โœจ FUNCTIONS โœจ

๐Ÿก home

use File::HomeDir;
$home = home();
$home = home('foo');
$home = File::HomeDir::home();
$home = File::HomeDir::home('foo');

The home function is exported by default and is provided for compatibility with legacy applications. ๐Ÿ‘ด In new applications, you should use the newer method-based interface above โ€” that's the main character energy choice. ๐Ÿ‘‘

Returns the directory path to a named user's home/profile directory.

If provided no parameter, returns the directory path to the current user's home/profile directory.


๐ŸŒˆ TO DO ๐ŸŒˆ

  • Add more granularity to Unix, and add support to VMS and other esoteric platforms, so we can consider going core. ๐Ÿ”ง
  • Add consistent support for users_* methods. ๐Ÿ“‹

๐Ÿซถ SUPPORT ๐Ÿซถ

This module is stored in an Open Repository at the following address.

http://svn.ali.as/cpan/trunk/File-HomeDir

Write access to the repository is made available automatically to any published 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 strongly encouraged to do so โ€” because the author currently maintains over 100 modules and it can take some time to deal with non-Critical bug reports or patches. ๐Ÿ˜ญ they're busy bestie.

This will guarantee that your issue will be addressed in the next release 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 CPAN bug tracker.

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-HomeDir

For other issues, for commercial enhancement or support, or to have your write access enabled for the repository, contact the author at the email address above. ๐Ÿ“ฌ


๐ŸŽ‰ ACKNOWLEDGEMENTS ๐ŸŽ‰

The biggest acknowledgement goes to Chris Nandor, who wielded his legendary Mac-fu and turned the initial fairly ordinary Darwin implementation into something that actually worked properly everywhere โ€” and then donated a Mac OS X license to allow it to be maintained properly. ๐Ÿ‘‘๐Ÿš absolute icon behaviour, no cap.


๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

  • Adam Kennedy <adamk@cpan.org>
  • Sean M. Burke <sburke@cpan.org>
  • Chris Nandor <cnandor@cpan.org>
  • Stephen Steneker <stennie@cpan.org>

โœจ SEE ALSO โœจ

ok so if you're done here and you still want more file-system content (and honestly? same), go check these besties out ๐Ÿ’–

  • File::ShareDir โ€” the sibling module, it does the share-dir thing and it's lowkey iconic for it ๐ŸŽ€
  • File::HomeDir::Win32 โ€” the legacy one, yes it's giving "vintage", yes it still exists, treat it with respect ๐Ÿ’…

ok legal era, but like... respectfully, these people ate and we should say thank you ๐Ÿ’–

  • Copyright 2005 - 2012 Adam Kennedy.
  • Copyright 2017 - 2020 Jens Rehsack
  • Some parts copyright 2000 Sean M. Burke.
  • Some parts copyright 2006 Chris Nandor.
  • Some parts copyright 2006 Stephen Steneker.
  • Some parts copyright 2009-2011 Jรฉrรดme Quelin.

no cap, this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself ๐Ÿซถ that's the whole deal, fr fr.

the full text of the license can be found in the LICENSE file included with this module โ€” like actually go read it if you need the details, it's right there bestie ๐Ÿ“„โœจ


ok that's the whole page!! you made it to the end, main character energy fr fr ๐Ÿ‘‘๐Ÿ’œ

and that's a wrap on this absolute icon of a manpage โ€” perl v5.42.0, dated 2020-09-29 ๐ŸŒˆ go forth and find your home directory, bestie, you've earned it ๐Ÿฆ‹๐Ÿ’–

๐Ÿ“ฆ where this page comes from