The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

ReadDir - Get the inode numbers in your readdir call.

SYNOPSIS

  use ReadDir qw(&readdir_inode);

  my (@files) = readdir_inode ".";

  printf ("%7d %s\n", $_->[1], $_->[0])
      foreach (@files);

DESCRIPTION

readdir_inode is a lot like the builtin readdir, but this function returns the inode numbers of the directory entries as well. If it is returned by your system, the contents of the "d_type" field are returned as well.

So, the example in the synopsis is a quick `ls -i'.

This will save you a `stat' in certain situations.

I didn't implement the whole opendir/readdir/closedir system, because I think that's an overcomplication; but see IO::Dirent for a replacement of the readdir() function that works with opendir.

FUNCTIONS

readdir_inode($dir)

Returns an ARRAY of [ $name, $inode ] for the given directory.

readdir_arrayref($dir)

Returns a reference to an ARRAY of [ $name, $inode ] for the passed directory.

readdir_hashref($dir)

Returns a reference to a HASH consisting of ($name => $inode) pairs.

FURTHER EXAMPLES

 use ReadDir;
 $contents = readdir_hashref(".");

 delete $contents->{"."};
 delete $contents->{".."};

 while (my ($filename, $inode) = each %$contents) {
     printf ("%7d %s\n", $inode, $filename);
 }

 $contents = readdir_arrayref("..");
 for my $dent (@$contents) {
     printf ("%7d %s\n", $dent->[1], $dent->[2]);
 }

CAVEATS

If the directory entry in question is a mount point, you will receive the inode number of the underlying directory, not the root inode of the mounted filesystem. This behaviour may or may not vary between systems.

This may not be a very portable function, either. It works on at least Linux, Solaris, and FreeBSD. It does not return anything useful on Windows based platforms.

Remember, the operating system keeps its own cache of directory entries. Consider whether or not you are just adding to complete system bloat using this function :-).

AUTHOR

Sam Vilain, <sam@vilain.net>

Many thanks to Ville Herva for debugging an XS memory leak for me, and providing readdir_hashref!

SEE ALSO

perlfunc. IO::Dirent provides an alternate approach to the same thing that