Madam has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, Below is my code for finding whether particular path is present or not.
use strict; use File::Spec; my $str = " /home/madam ~/madam"; my @strarray = split(" ",$str); for (@strarray){ if ( -d $_){ print "path present : $_\n"; } else { print "path not present : $_\n"; } }
the output is : path present : /home/madam path not present : ~/madam
but both paths are same. why it is not understanding ~/madam? Thanks, Madam

Replies are listed 'Best First'.
Re: how to expand home directory ~
by derby (Abbot) on Jun 02, 2005 at 12:26 UTC

    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";
    -derby
    Update and to combine the regex with your code:
    #!/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 }
      Probably the same thing as File::Path::Expand

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

        They're pretty close but it looks like File::Path::Expand chokes on the simple cases of ~name and just ~. Not that the perlfaq regex is any better, it chokes when $ENV{HOME} and $ENV{LOGDIR} are not set. A better approach would be something along the lines of:

        $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] : (getpwuid($<))[7] }ex;
        -derby
Re: how to expand home directory ~
by Joost (Canon) on Jun 02, 2005 at 12:05 UTC
Re: how to expand home directory ~
by muntfish (Chaplain) on Jun 02, 2005 at 12:09 UTC

    Those 2 paths are not the same:

    • /home/madam is the home directory of user madam.
    • ~/madam is a subdirectory of the current user's home directory. The current user may not be madam.

    Maybe you made a typo and you meant ~madam not ~/madam - in which case they should be the same...


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&

      Of course my previous post may be irrelevant, as Joost points out, Perl doesn't expand ~ anyway...

      There are a few different ways to expand ~, if you need to:

      • Globbing - either <~madam> or glob('~madam') should both work, or you can look at File::Glob.
      • Regexp to replace ~ with $ENV{HOME} assuming $HOME is set (it almost always is) on your Unix environment. That only works if you're interested in the current user's home dir of course.
      • Some other module may do it - File::Spec doesn't but there are heaps of other modules under File:: on CPAN!

      Hope that helps!


      s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&