If ~ really is hardcoded in the script, you'll need to expand it first. perlfaq5 has a nice regex for doing just that:
$filename = "~/madam"; $filename =~ s{ ^ ~ # find a leading tilde ( # save this in $1 [^/] # a non-slash character * # repeated 0 or more times (0 means me) ) }{ $1 ? (getpwnam($1))[7] : ( $ENV{HOME} || $ENV{LOGDIR} ) }ex; print $filename, "\n";
#!/usr/bin/perl use strict; use File::Spec; my $str = " /home/madam ~madam ~/madam"; my @strarray = split(" ",$str); for (@strarray){ my $file = expand_tilde( $_ ); if ( -d $file ){ print "path present : $file\n"; } else { print "path not present : $file\n"; } } sub expand_tilde { my( $file ) = shift; $file =~ s{ ^ ~ # find a leading tilde ( # save this in $1 [^/] # a non-slash character * # repeated 0 or more times (0 means me) ) }{ $1 ? (getpwnam($1))[7] : ( $ENV{HOME} || $ENV{LOGDIR} ) }ex; $file }
In reply to Re: how to expand home directory ~
by derby
in thread how to expand home directory ~
by Madam
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |