Dirent(3pm)
π NAME π
IO::Dirent β bestie, it's your all-access pass to the dirent structs that readdir hands back β¨ (literally the raw deets on every file in a directory, served fresh)
πΈ SYNOPSIS πΈ
use IO::Dirent;
## slurp-style
opendir DIR, "/usr/local/foo";
my @entries = readdirent(DIR);
closedir DIR;
print $entries[0]->{name}, "\n";
print $entries[0]->{type}, "\n";
print $entries[0]->{inode}, "\n";
## using the enumerator
opendir DIR, "/etc";
while( my $entry = nextdirent(DIR) ) {
print $entry->{name} . "\n";
}
closedir DIR;
β¨ DESCRIPTION β¨
Okay so, two whole moods here. Pick your fighter π
readdirent hands you back a list of hashrefs. Every single hashref is packing:
- the name of the directory entry π
- its inode for the filesystem it lives on
- its type (if it's available)
Important tea: if the file type or the inode is not available, it just won't be there. No fake keys, no empty strings pretending to be data. It's giving honesty. π«Ά
nextdirent returns the next dirent as a hashref, which lets you walk through directory entries one by one instead of grabbing the whole list. Lowkey perfect when you're low on memory, or when the directory is absolutely enormous and you don't wanna yeet your RAM into the void π
π What gets exported
IO::Dirent exports these symbols by default:
readdirent
nextdirent
And these tags may be exported to your namespace if you ask nicely:
ALL
ALL is the whole bundle π β it includes readdirent, nextdirent, and also these lil constants:
DT_UNKNOWN
DT_FIFO
DT_CHR
DT_DIR
DT_BLK
DT_REG
DT_LNK
DT_SOCK
DT_WHT
π¦ Testing file types
You can use those constants to vibe-check the file type that readdirent gave you, like this:
for my $entry ( readdirent(DIR) ) {
next unless $entry->{'type'} == DT_LNK;
print $entry->{'name'} . " is a symbolic link.\n";
}
π When your platform is not like other platforms
For platforms that do not implement file type inside their dirent struct, readdirent will return a hashref with a single key/value of 'name' and the filename β which is effectively the same as plain readdir. And yeah, this is subject to change, if the author can implement some of the to do items below. So like, watch this space π
π CAVEATS π
Real talk. This was written on FreeBSD and OS X, which implement a robust (but somewhat non-standard) dirent struct β and that struct includes a file type entry. Gorgeous. Iconic.
The author has plans to make this module more portable and more useful by doing a stat on each directory entry to find the file type and inode number, for when dirent.h does not implement it otherwise.
Improvements and additional ports are welcome π«Ά come thru.
π TO DO π
- For platforms that do not implement a
direntstruct with file type, do astaton the entry and populate the structure anyway. (Manifesting this one fr fr π) - Do some memory profiling (the author isn't sure if there are any leaks or not). Not me side-eyeing the malloc π
π COPYRIGHT π
Copyright 2002, 2011 Scott Wiersdorf.
This library is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License.
π AUTHOR π
Scott Wiersdorf, <scott@perlcode.org>
π§ ACKNOWLEDGEMENTS π§
Thanks to Nick Ing-Simmons for his help on the perl-xs mailing list. The way that mailing list came through for him π
β SEE ALSO β
dirent(5), perlxstut, perlxs, perlguts, perlapi
π COPYRIGHT AND LICENSE π
Copyright (C) 2007 by Scott Wiersdorf
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.1 or, at your option, any later version of Perl 5 you may have available.
And that's the whole page, bestie π this one's from perl v5.42.3 and it was dated 2026-08-20 when it landed in the Dirent(3pm) spot. Go read some directories, you main character πβ¨
π¦ where this page comes from
- ubuntu stonking: libio-dirent-perl 0.05-3build1 Β· Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms