Underappreciated Perl Modules: File::HomeDir
2013-09-03
Problem: your module needs to save a config file. Where should it go?
Obviously, the user’s homedir, but the exact conventions differ between platforms. On Unixy things, it’s usually a dot file. Windows has a more complex layout for User directories, which Microsoft seems to change with every new version.
Instead of guessing, ask File::HomeDir where it should go. UAV::Pilot needed this for the joystick config, so it does something like this:
use File::HomeDir;
use File::Spec;
my $dir = File::HomeDir->my_dist_config( 'UAV-Pilot', {
create => 1,
});
my $file = File::Spec->catfile( $dir, 'sdl_joystick.yml' );
File::HomeDir
also has functions for my_music
, my_pictures
, etc., and also for getting the homedir of another user. It should always Do the Right Thing on any platform you throw at it.