File::Path::Expand - expand filenames


File-Path-Expand documentation Contained in the File-Path-Expand distribution.

Index


Code Index:

NAME

Top

File::Path::Expand - expand filenames

SYNOPSIS

Top

 use File::Path::Expand;
 print expand_filename("~richardc/foo"); # prints "/home/richardc/foo"

DESCRIPTION

Top

File::Path::Expand expands user directories in filenames. For the simple case it's no more complex than s{^~/}{$HOME/}, but for other cases it consults getpwent and does the right thing.

AUTHOR

Top

Richard Clamp <richardc@unixbeard.net>

COPYRIGHT

Top


File-Path-Expand documentation Contained in the File-Path-Expand distribution.

use strict;
package File::Path::Expand;
use User::pwent;
use Carp qw(croak);
use Exporter;
use base 'Exporter';
use vars qw( $VERSION @EXPORT @EXPORT_OK );

$VERSION   = '1.02';
@EXPORT    = qw( expand_filename );
@EXPORT_OK = qw( expand_filename  home_of );

sub expand_filename {
    my $path = shift;
    $path =~ s{^~(?=/|$)}{ $ENV{HOME} ? "$ENV{HOME}" : home_of( $> ) }e
      or $path =~ s{^~(.+?)(?=/|$)}{ home_of( $1 ) }e;
    return $path;
}

sub home_of {
    my $user = shift;
    my $ent = getpw($user)
      or croak "no such user '$user'";
    return $ent->dir;
}

1;
__END__