1use strict;
2package File::Path::Expand;
3use User::pwent;
4use Carp qw(croak);
5use Exporter;
6use base 'Exporter';
7use vars qw( $VERSION @EXPORT @EXPORT_OK );
8
9$VERSION   = '1.02';
10@EXPORT    = qw( expand_filename );
11@EXPORT_OK = qw( expand_filename  home_of );
12
13sub expand_filename {
14    my $path = shift;
15    $path =~ s{^~(?=/|$)}{ $ENV{HOME} ? "$ENV{HOME}" : home_of( $> ) }e
16      or $path =~ s{^~(.+?)(?=/|$)}{ home_of( $1 ) }e;
17    return $path;
18}
19
20sub home_of {
21    my $user = shift;
22    my $ent = getpw($user)
23      or croak "no such user '$user'";
24    return $ent->dir;
25}
26
271;
28__END__
29
30=head1 NAME
31
32File::Path::Expand - expand filenames
33
34=head1 SYNOPSIS
35
36 use File::Path::Expand;
37 print expand_filename("~richardc/foo"); # prints "/home/richardc/foo"
38
39=head1 DESCRIPTION
40
41File::Path::Expand expands user directories in filenames.  For the
42simple case it's no more complex than s{^~/}{$HOME/}, but for other
43cases it consults C<getpwent> and does the right thing.
44
45=head1 AUTHOR
46
47Richard Clamp <richardc@unixbeard.net>
48
49=head1 COPYRIGHT
50
51Copyright (c) 2003, Richard Clamp. All Rights Reserved.  This module
52is free software. It may be used, redistributed and/or modified under
53the same terms as Perl itself.
54
55=cut
56