xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/DirHandle.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage DirHandle;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateour $VERSION = '1.00';
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate=head1 NAME
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateDirHandle - supply object methods for directory handles
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate=head1 SYNOPSIS
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate    use DirHandle;
12*0Sstevel@tonic-gate    $d = new DirHandle ".";
13*0Sstevel@tonic-gate    if (defined $d) {
14*0Sstevel@tonic-gate        while (defined($_ = $d->read)) { something($_); }
15*0Sstevel@tonic-gate        $d->rewind;
16*0Sstevel@tonic-gate        while (defined($_ = $d->read)) { something_else($_); }
17*0Sstevel@tonic-gate        undef $d;
18*0Sstevel@tonic-gate    }
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate=head1 DESCRIPTION
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gateThe C<DirHandle> method provide an alternative interface to the
23*0Sstevel@tonic-gateopendir(), closedir(), readdir(), and rewinddir() functions.
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gateThe only objective benefit to using C<DirHandle> is that it avoids
26*0Sstevel@tonic-gatenamespace pollution by creating globs to hold directory handles.
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate=head1 NOTES
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate=over 4
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate=item *
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gateOn Mac OS (Classic), the path separator is ':', not '/', and the
35*0Sstevel@tonic-gatecurrent directory is denoted as ':', not '.'. You should be careful
36*0Sstevel@tonic-gateabout specifying relative pathnames. While a full path always begins
37*0Sstevel@tonic-gatewith a volume name, a relative pathname should always begin with a
38*0Sstevel@tonic-gate':'.  If specifying a volume name only, a trailing ':' is required.
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate=back
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate=cut
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gaterequire 5.000;
45*0Sstevel@tonic-gateuse Carp;
46*0Sstevel@tonic-gateuse Symbol;
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gatesub new {
49*0Sstevel@tonic-gate    @_ >= 1 && @_ <= 2 or croak 'usage: new DirHandle [DIRNAME]';
50*0Sstevel@tonic-gate    my $class = shift;
51*0Sstevel@tonic-gate    my $dh = gensym;
52*0Sstevel@tonic-gate    if (@_) {
53*0Sstevel@tonic-gate	DirHandle::open($dh, $_[0])
54*0Sstevel@tonic-gate	    or return undef;
55*0Sstevel@tonic-gate    }
56*0Sstevel@tonic-gate    bless $dh, $class;
57*0Sstevel@tonic-gate}
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gatesub DESTROY {
60*0Sstevel@tonic-gate    my ($dh) = @_;
61*0Sstevel@tonic-gate    closedir($dh);
62*0Sstevel@tonic-gate}
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gatesub open {
65*0Sstevel@tonic-gate    @_ == 2 or croak 'usage: $dh->open(DIRNAME)';
66*0Sstevel@tonic-gate    my ($dh, $dirname) = @_;
67*0Sstevel@tonic-gate    opendir($dh, $dirname);
68*0Sstevel@tonic-gate}
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gatesub close {
71*0Sstevel@tonic-gate    @_ == 1 or croak 'usage: $dh->close()';
72*0Sstevel@tonic-gate    my ($dh) = @_;
73*0Sstevel@tonic-gate    closedir($dh);
74*0Sstevel@tonic-gate}
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gatesub read {
77*0Sstevel@tonic-gate    @_ == 1 or croak 'usage: $dh->read()';
78*0Sstevel@tonic-gate    my ($dh) = @_;
79*0Sstevel@tonic-gate    readdir($dh);
80*0Sstevel@tonic-gate}
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gatesub rewind {
83*0Sstevel@tonic-gate    @_ == 1 or croak 'usage: $dh->rewind()';
84*0Sstevel@tonic-gate    my ($dh) = @_;
85*0Sstevel@tonic-gate    rewinddir($dh);
86*0Sstevel@tonic-gate}
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate1;
89