Perl Advent Calendar 2006-12-15

I'll Be Home for Christmas

by Jerrad Pierce

So, you need to access files in a user's home directory to parse a resource file, replace his inbox with a can of SPAM, etc. etc. Sure you could try using $HOME (if it's set, good luck on Win32) or getpwent (Win32 loses again) or even

This last one shows promise, until you try to get the home of another user e.g; ~administrator (on everyone's favorite platform). If all of this is reminiscent of coping with the joining, splitting and other -ings of paths pre-File::Spec then you might wonder why there's nothing along the lines of a File::Spec::Home to do all of the heavy lifting.

Presenting the all new, redesigned 2006 model File::HomeDir. This module provides a platform-agnostic framework for determining the location of a user's home directory, and other possible special folders such as /(?:My )?(?:Documents/Music|Videos)/. File::HomeDir still needs additional code though, to detect and report the location of these and the user's desktop on the Unices, where they vary between KDE, Gnome, and other environments. Also note that until 0.60 goes gold, you need to install the developer's release (currently 0.60_6) in order for the users_* methods like below to work properly; they were left out of the base class in 0.58 but the platform specific implementations can be called directly.

   1 #!/usr/bin/perl -U
   2 use File::HomeDir;
   4 unlink( File::HomeDir->users_home('w') );