ReadDir - Get the inode numbers in your readdir call.
use ReadDir qw(&readdir_inode); my (@files) = readdir_inode "."; printf ("%7d %s\n", $_->[1], $_->[0]) foreach (@files);
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'.
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.
Returns an ARRAY of [ $name, $inode ] for the given directory.
[ $name, $inode ]
Returns a reference to an ARRAY of [ $name, $inode ] for the passed directory.
Returns a reference to a HASH consisting of ($name => $inode) pairs.
($name => $inode)
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]); }
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 :-).
Sam Vilain, <sam@vilain.net>
Many thanks to Ville Herva for debugging an XS memory leak for me, and providing readdir_hashref!
perlfunc. IO::Dirent provides an alternate approach to the same thing that
To install ReadDir, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ReadDir
CPAN shell
perl -MCPAN -e shell install ReadDir
For more information on module installation, please visit the detailed CPAN module installation guide.