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

I've got this script:
#!/usr/bin/perl my $dirtoread="/home/mysite/public_html/t/d/"; opendir(IDIR, $dirtoread) || die "Error Opening Directory"; print "Content-type: text/html\n\n"; foreach $file ( sort readdir(IDIR) ) { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$bl +ksize,$blocks) = stat($file); print <<EOF; File: $file dev: $dev ino: $ino mode: $mode nlink: $nlink uid: $uid gid: $gid rdev: $rdev size: $size atime: $atime mtime: $mtime ctime: $ctime blksize: $blksize blocks: $blocks } closedir(DIR);


Here is the output:
File: . dev: 771 ino: 5915501 mode: 16877 nlink: 2 uid: 32088 gid: 590 rdev: 0 size: 4096 atime: 1115011633 mtime: 1115011632 ctime: 1115011632 blksize: 4096 blocks: 8 File: .. dev: 771 ino: 1507821 mode: 16877 nlink: 5 uid: 32088 gid: 590 rdev: 0 size: 4096 atime: 1115009208 mtime: 1115009208 ctime: 1115009208 blksize: 4096 blocks: 8 File: files dev: ino: mode: nlink: uid: gid: rdev: size: atime: mtime: ctime: blksize: blocks: File: fileviewer.pl dev: 771 ino: 5915502 mode: 33261 nlink: 1 uid: 32088 gid: 590 rdev: 0 size: 616 atime: 1115011637 mtime: 1115011632 ctime: 1115011632 blksize: 4096 blocks: 8 File: index.html dev: ino: mode: nlink: uid: gid: rdev: size: atime: mtime: ctime: blksize: blocks:

The directory has a subdirectory called "files" and two files called "index.html" and "fileviewer.pl".

I see why nothing is coming back for "files" because it's a directory but why is "index.html" not coming back with any data?

I'm trying to list the Files and Directories in the current directory. For the Directories I'd like to know that it is a directory because I'm going to create a HTML link on it so people can browse around my server directory.

Replies are listed 'Best First'.
Re: Why is Stat() Not Stating All Files?
by merlyn (Sage) on May 02, 2005 at 05:39 UTC
    As the FM says:
    If you're planning to filetest the return values out of a "readdir", you'd better prepend the directory in question. Otherwise, because we didn't "chdir" there, it would have +been testing the wrong file. opendir(DIR, $some_dir) || die "can't opendir $some_di +r: $!"; @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DI +R); closedir DIR;

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Okay, I changed my code:

      #!/usr/bin/perl my $dirtoread="/home/mydir/public_html/t/d/"; opendir(IDIR, $dirtoread) || die "Error Opening Directory"; print "Content-type: text/html\n\n"; my @dots = grep { /^\./ && -f "$dirtoread/$_" } readdir(IDIR); foreach $file (@dots) { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$bl +ksize,$blocks) = stat($file); print <<EOF; File: $file<br> dev: $dev<br> ino: $ino<br> mode: $mode<br> nlink: $nlink<br> uid: $uid<br> gid: $gid<br> rdev: $rdev<br> size: $size<br> atime: $atime<br> mtime: $mtime<br> ctime: $ctime<br> blksize: $blksize<br> blocks: $blocks<br><br><br> EOF } closedir(IDIR);

      But it's not printing anything?
      Did I apply the code correctly?

      I don't think I did but I'm not really sure what the deal is!
        Did I apply the code correctly?
        No. You're still testing the wrong file, you still need to prepend the directory in question, only now you filter out directories from the results of readdir (you're left with files only).

        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.

Re: Why is Stat() Not Stating All Files?
by Zaxo (Archbishop) on May 02, 2005 at 05:54 UTC

    You can also glob to get the names of files:

    for (glob $dirtoread . '*') { my @stats = stat; # . . . }
    That will eliminate the dot directories from consideration, and the path to the files will be part of the names in the list. You might want to make print conditional on -f _ to eliminate directories from output.

    After Compline,
    Zaxo

Re: Why is Stat() Not Stating All Files?
by Fletch (Bishop) on May 02, 2005 at 13:32 UTC

    And no one else has mentioned this explicitly, but stat works perfectly well on directories as your own results for ./ show. Some of the results have a little different meaning, but the system call itself works fine.

Re: Why is Stat() Not Stating All Files?
by polettix (Vicar) on May 02, 2005 at 14:28 UTC
    After fixing errors as pointed out by others, also take note that working with symbolic links could result in some surprise, and you should take a look to perldoc -f lstat. This is particularly needed when your link points nowhere.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.