Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.3 KiB

  1. package DirHandle;
  2. =head1 NAME
  3. DirHandle - supply object methods for directory handles
  4. =head1 SYNOPSIS
  5. use DirHandle;
  6. $d = new DirHandle ".";
  7. if (defined $d) {
  8. while (defined($_ = $d->read)) { something($_); }
  9. $d->rewind;
  10. while (defined($_ = $d->read)) { something_else($_); }
  11. undef $d;
  12. }
  13. =head1 DESCRIPTION
  14. The C<DirHandle> method provide an alternative interface to the
  15. opendir(), closedir(), readdir(), and rewinddir() functions.
  16. The only objective benefit to using C<DirHandle> is that it avoids
  17. namespace pollution by creating globs to hold directory handles.
  18. =cut
  19. require 5.000;
  20. use Carp;
  21. use Symbol;
  22. sub new {
  23. @_ >= 1 && @_ <= 2 or croak 'usage: new DirHandle [DIRNAME]';
  24. my $class = shift;
  25. my $dh = gensym;
  26. if (@_) {
  27. DirHandle::open($dh, $_[0])
  28. or return undef;
  29. }
  30. bless $dh, $class;
  31. }
  32. sub DESTROY {
  33. my ($dh) = @_;
  34. closedir($dh);
  35. }
  36. sub open {
  37. @_ == 2 or croak 'usage: $dh->open(DIRNAME)';
  38. my ($dh, $dirname) = @_;
  39. opendir($dh, $dirname);
  40. }
  41. sub close {
  42. @_ == 1 or croak 'usage: $dh->close()';
  43. my ($dh) = @_;
  44. closedir($dh);
  45. }
  46. sub read {
  47. @_ == 1 or croak 'usage: $dh->read()';
  48. my ($dh) = @_;
  49. readdir($dh);
  50. }
  51. sub rewind {
  52. @_ == 1 or croak 'usage: $dh->rewind()';
  53. my ($dh) = @_;
  54. rewinddir($dh);
  55. }
  56. 1;