3 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
16 use vars qw(@ISA $VERSION @EXPORT_OK);
20 @ISA = qw(Tie::Hash Exporter);
22 @EXPORT_OK = qw(DIR_UNLINK);
24 sub DIR_UNLINK () { 1 }
27 @_ >= 1 && @_ <= 2 or croak 'usage: new IO::Dir [DIRNAME]';
31 IO::Dir::open($dh, $_[0])
43 @_ == 2 or croak 'usage: $dh->open(DIRNAME)';
44 my ($dh, $dirname) = @_;
46 unless opendir($dh, $dirname);
47 ${*$dh}{io_dir_path} = $dirname;
52 @_ == 1 or croak 'usage: $dh->close()';
58 @_ == 1 or croak 'usage: $dh->read()';
64 @_ == 2 or croak 'usage: $dh->seek(POS)';
70 @_ == 1 or croak 'usage: $dh->tell()';
76 @_ == 1 or croak 'usage: $dh->rewind()';
82 my($class,$dir,$options) = @_;
84 my $dh = $class->new($dir)
89 ${*$dh}{io_dir_unlink} = $options & DIR_UNLINK;
106 -e ${*$dh}{io_dir_path} . "/" . $key;
111 &lstat(${*$dh}{io_dir_path} . "/" . $key);
115 my($dh,$key,$data) = @_;
116 my($atime,$mtime) = ref($data) ? @$data : ($data,$data);
117 my $file = ${*$dh}{io_dir_path} . "/" . $key;
119 my $io = IO::File->new($file,O_CREAT | O_RDWR);
122 utime($atime,$mtime, $file);
127 # Only unlink if unlink-ing is enabled
128 my $file = ${*$dh}{io_dir_path} . "/" . $key;
131 unless ${*$dh}{io_dir_unlink};
144 IO::Dir - supply object methods for directory handles
149 $d = new IO::Dir ".";
151 while (defined($_ = $d->read)) { something($_); }
153 while (defined($_ = $d->read)) { something_else($_); }
157 tie %dir, IO::Dir, ".";
158 foreach (keys %dir) {
159 print $_, " " , $dir{$_}->size,"\n";
164 The C<IO::Dir> package provides two interfaces to perl's directory reading
167 The first interface is an object approach. C<IO::Dir> provides an object
168 constructor and methods, which are just wrappers around perl's built in
169 directory reading routines.
173 =item new ( [ DIRNAME ] )
175 C<new> is the constuctor for C<IO::Dir> objects. It accepts one optional
176 argument which, if given, C<new> will pass to C<open>
180 The following methods are wrappers for the directory related functions built
181 into perl (the trailing `dir' has been removed from the names). See L<perlfunc>
182 for details of these functions.
186 =item open ( DIRNAME )
200 C<IO::Dir> also provides a interface to reading directories via a tied
201 HASH. The tied HASH extends the interface beyond just the directory
202 reading routines by the use of C<lstat>, from the C<File::stat> package,
203 C<unlink>, C<rmdir> and C<utime>.
207 =item tie %hash, IO::Dir, DIRNAME [, OPTIONS ]
211 The keys of the HASH will be the names of the entries in the directory.
212 Reading a value from the hash will be the result of calling
213 C<File::stat::lstat>. Deleting an element from the hash will call C<unlink>
214 providing that C<DIR_UNLINK> is passed in the C<OPTIONS>.
216 Assigning to an entry in the HASH will cause the time stamps of the file
217 to be modified. If the file does not exist then it will be created. Assigning
218 a single integer to a HASH element will cause both the access and
219 modification times to be changed to that value. Alternatively a reference to
220 an array of two values can be passed. The first array element will be used to
221 set the access time and the second element will be used to set the modification
230 Graham Barr. Currently maintained by the Perl Porters. Please report all
231 bugs to <perl5-porters@perl.org>.
235 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
236 This program is free software; you can redistribute it and/or
237 modify it under the same terms as Perl itself.