Dear all,

I do some filetests on different directories. As expected, just using -filetest $file is not working. However, if I use Cwd's abs_path($file) I get the full path but the filetest fails. In contrast, concatenating the path and $file works, with and without abs_path(). Now I'm wondering if this is by purpose, a "bug" in filetest/Cwd or if I just miss something.

Cheers, LeJosh

I attached a minimal working example.

Perl version: perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi

Cwd version: 3.36

Running on: Linux: Linux dh2-biotek16 3.2.0-55-generic #85-Ubuntu SMP Wed Oct 2 12:29:27 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

use strict; use warnings; use Cwd 'abs_path'; my($path) = @ARGV; ## add path to a directory print "Input path: $path\n"; print "## no modifications to readdir output (as mentioned in the docs + this is bad for filetesting)\n"; opendir(my $dh, $path) or die "Cannot open dir $path: $!\n"; while(readdir($dh)) { #print "Testing $_\n"; if(-z $_) { print "File $_ is empty\n\n"; } elsif(-d $_) { print "File $_ is a directory\n\n"; } elsif(-f $_) { print "File $_ is a plain file\n\n"; } elsif(-B $_) { print "Path $_ points\tto a binary file\n\n"; } else { print "Not recognized: $_\n\n"; } } closedir($dh); print "## using abs_path\n"; opendir(my $dh1, $path) or die "Cannot open dir $path: $!\n"; while(readdir($dh1)) { my $abs_path = abs_path($_); #print "Testing $abs_path\n"; if(-z $abs_path) { print "Path $abs_path\tpoints to an empty file\n\n"; } elsif(-d $abs_path) { print "Path $abs_path\tis a directory\n\n"; } elsif(-f $abs_path) { print "Path $abs_path points\tto a plain file\n\n"; } elsif(-B $abs_path) { print "Path $abs_path points\tto a binary file\n\n"; } else { print "Not recognized: $abs_path\n\n"; } } closedir($dh1); print "## using $path/filename\n"; opendir(my $dh2, $path) or die "Cannot open dir $path: $!\n"; while(readdir($dh2)) { my $abs_path_mod = abs_path("$path/$_"); #print "Testing $abs_path_mod\n"; if(-z $abs_path_mod) { print "Path $abs_path_mod\tpoints to an empty file\n\n"; } elsif(-d $abs_path_mod) { print "Path $abs_path_mod\tis a directory\n\n"; } elsif(-f $abs_path_mod) { print "Path $abs_path_mod points\tto a plain file\n\n"; } elsif(-B $abs_path_mod) { print "Path $abs_path_mod points\tto a binary file\n"; } else { print "Not recognized: $abs_path_mod\n"; } } closedir($dh2); print "## using $path/filename w/o abs_path\n"; opendir(my $dh3, $path) or die "Cannot open dir $path: $!\n"; while(readdir($dh3)) { my $path_mod = "$path/$_"; #print "Testing $path_mod\n"; if(-z $path_mod) { print "Path $path_mod\tpoints to an empty file\n\n"; } elsif(-d $path_mod) { print "Path $path_mod\tis a directory\n\n"; } elsif(-f $path_mod) { print "Path $path_mod points\tto a plain file\n\n"; } elsif(-B $path_mod) { print "Path $path_mod points\tto a binary file\n"; } else { print "Not recognized: $path_mod\n"; } } closedir($dh3);

In reply to Cwd's abs_path not working with filetests? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.