| 1 |
NAME |
| 2 |
IO::Dirent - Access to dirent structs returned by readdir |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use IO::Dirent; |
| 6 |
|
| 7 |
opendir DIR, "/usr/local/foo"; |
| 8 |
my @entries = readdirent(DIR); |
| 9 |
closedir DIR; |
| 10 |
|
| 11 |
print $entries[0]->{name}, "\n"; |
| 12 |
print $entries[0]->{type}, "\n"; |
| 13 |
print $entries[0]->{inode}, "\n"; |
| 14 |
|
| 15 |
DESCRIPTION |
| 16 |
IO::Dirent exports the following symbols by default: |
| 17 |
|
| 18 |
readdirent nextdirent |
| 19 |
|
| 20 |
readdirent returns a list of hashrefs. Each hashref contains the |
| 21 |
name of the directory entry, its inode for the filesystem it |
| 22 |
resides on and its type (if available). If the file type or inode |
| 23 |
are not available, it won't be there! |
| 24 |
|
| 25 |
nextdirent is an enumerator for a directory handle. |
| 26 |
|
| 27 |
The following tags may be exported to your namespace: |
| 28 |
|
| 29 |
ALL |
| 30 |
|
| 31 |
which includes readdirent and the following symbols: |
| 32 |
|
| 33 |
DT_UNKNOWN |
| 34 |
DT_FIFO |
| 35 |
DT_CHR |
| 36 |
DT_DIR |
| 37 |
DT_BLK |
| 38 |
DT_REG |
| 39 |
DT_LNK |
| 40 |
DT_SOCK |
| 41 |
DT_WHT |
| 42 |
|
| 43 |
These symbols can be used to test the file type returned by readdirent |
| 44 |
in the following manner: |
| 45 |
|
| 46 |
for my $entry ( readdirent(DIR) ) { |
| 47 |
next unless $entry->{'type'} == DT_LNK; |
| 48 |
|
| 49 |
print $entry->{'name'} . " is a symbolic link.\n"; |
| 50 |
} |
| 51 |
|
| 52 |
Or by nextdirent in the same way: |
| 53 |
|
| 54 |
while( my $entry = nextdirent(DIR) ) { |
| 55 |
next unless $entry->{'type'} == DT_LNK; |
| 56 |
|
| 57 |
print $entry->{'name'} . " is a symbolic link.\n"; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
For platforms that do not implement file type in its dirent struct, |
| 62 |
readdirent will return a hashref with a single key/value of 'name' and |
| 63 |
the filename (effectively the same as readdir). This is subject to |
| 64 |
change, if I can implement some of the to do items below. |
| 65 |
|
| 66 |
CAVEATS |
| 67 |
This was written on FreeBSD and OS X which implement a robust (but |
| 68 |
somewhat non-standard) dirent struct and which includes a file |
| 69 |
type entry. I have plans to make this module more portable and |
| 70 |
useful by doing a stat on each directory entry to find the file |
| 71 |
type and inode number when the dirent.h does not implement it |
| 72 |
otherwise. |
| 73 |
|
| 74 |
Improvements and additional ports are welcome. |
| 75 |
|
| 76 |
BUILDING |
| 77 |
|
| 78 |
On OS X and recent Apple hardware, the undocumented ARCHFLAGS |
| 79 |
environment variable was valuable; without it, Perl would attempt |
| 80 |
to build i386 and ppc versions (which didn't work on my hardware): |
| 81 |
|
| 82 |
ARCHFLAGS="-arch x86_64" perl Makefile.PL |
| 83 |
|
| 84 |
I'd be happy to know if anyone knows of a way to detect this and |
| 85 |
set it during Makefile.PL. |
| 86 |
|
| 87 |
TO DO |
| 88 |
* For platforms that do not implement a dirent struct with file type, |
| 89 |
do a stat on the entry and populate the structure anyway. |
| 90 |
|
| 91 |
* Do some memory profiling (I'm not sure if I have any leaks or not). |
| 92 |
|
| 93 |
COPYRIGHT |
| 94 |
Copyright 2002, 2011 Scott Wiersdorf. |
| 95 |
|
| 96 |
This library is free software; you can redistribute it and/or modify it |
| 97 |
under the terms of the Perl Artistic License. |
| 98 |
|
| 99 |
AUTHOR |
| 100 |
Scott Wiersdorf, <[email protected]> |
| 101 |
|
| 102 |
ACKNOWLEDGEMENTS |
| 103 |
Thanks to Nick Ing-Simmons for his help on the perl-xs mailing list. |
| 104 |
|
| 105 |
SEE ALSO |
| 106 |
dirent(5), the perlxstut manpage, the perlxs manpage, the perlguts |
| 107 |
manpage, the perlapi manpage |
| 108 |
|