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