7 DirHandle - (obsolete) supply object methods for directory handles
11 # recommended approach since Perl 5.6: do not use DirHandle
12 if (opendir my $d, '.') {
13 while (readdir $d) { something($_); }
15 while (readdir $d) { something_else($_); }
18 # how you would use this module if you were going to
20 if (my $d = DirHandle->new(".")) {
21 while (defined($_ = $d->read)) { something($_); }
23 while (defined($_ = $d->read)) { something_else($_); }
28 B<There is no reason to use this module nowadays.>
30 The C<DirHandle> method provide an alternative interface to the
31 opendir(), closedir(), readdir(), and rewinddir() functions.
33 Up to Perl 5.5, opendir() could not autovivify a directory handle from
34 C<undef>, so using a lexical handle required using a function from L<Symbol>
35 to create an anonymous glob, which took a separate step.
36 C<DirHandle> encapsulates this, which allowed cleaner code than opendir().
37 Since Perl 5.6, opendir() alone has been all you need for lexical handles.
46 @_ >= 1 && @_ <= 2 or croak 'usage: DirHandle->new( [DIRNAME] )';
50 DirHandle::open($dh, $_[0])
58 # Don't warn about already being closed as it may have been closed
59 # correctly, or maybe never opened at all.
60 local($., $@, $!, $^E, $?);
66 @_ == 2 or croak 'usage: $dh->open(DIRNAME)';
67 my ($dh, $dirname) = @_;
68 opendir($dh, $dirname);
72 @_ == 1 or croak 'usage: $dh->close()';
78 @_ == 1 or croak 'usage: $dh->read()';
84 @_ == 1 or croak 'usage: $dh->rewind()';