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 our(@ISA, $VERSION, @EXPORT_OK);
21 @ISA = qw(Tie::Hash Exporter);
23 @EXPORT_OK = qw(DIR_UNLINK);
25 sub DIR_UNLINK
() { 1 }
28 @_ >= 1 && @_ <= 2 or croak
'usage: new IO::Dir [DIRNAME]';
32 IO
::Dir
::open($dh, $_[0])
44 @_ == 2 or croak
'usage: $dh->open(DIRNAME)';
45 my ($dh, $dirname) = @_;
47 unless opendir($dh, $dirname);
48 # a dir name should always have a ":" in it; assume dirname is
49 # in current directory
50 $dirname = ':' . $dirname if ( ($^O
eq 'MacOS') && ($dirname =~ /^[^:]+$/) );
51 ${*$dh}{io_dir_path
} = $dirname;
56 @_ == 1 or croak
'usage: $dh->close()';
62 @_ == 1 or croak
'usage: $dh->read()';
68 @_ == 2 or croak
'usage: $dh->seek(POS)';
74 @_ == 1 or croak
'usage: $dh->tell()';
80 @_ == 1 or croak
'usage: $dh->rewind()';
86 my($class,$dir,$options) = @_;
88 my $dh = $class->new($dir)
93 ${*$dh}{io_dir_unlink
} = $options & DIR_UNLINK
;
110 -e File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key);
115 &lstat(File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key));
119 my($dh,$key,$data) = @_;
120 my($atime,$mtime) = ref($data) ? @
$data : ($data,$data);
121 my $file = File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key);
123 my $io = IO
::File
->new($file,O_CREAT
| O_RDWR
);
126 utime($atime,$mtime, $file);
131 # Only unlink if unlink-ing is enabled
132 my $file = File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key);
135 unless ${*$dh}{io_dir_unlink
};
148 IO::Dir - supply object methods for directory handles
153 $d = new IO::Dir ".";
155 while (defined($_ = $d->read)) { something($_); }
157 while (defined($_ = $d->read)) { something_else($_); }
161 tie %dir, IO::Dir, ".";
162 foreach (keys %dir) {
163 print $_, " " , $dir{$_}->size,"\n";
168 The C<IO::Dir> package provides two interfaces to perl's directory reading
171 The first interface is an object approach. C<IO::Dir> provides an object
172 constructor and methods, which are just wrappers around perl's built in
173 directory reading routines.
177 =item new ( [ DIRNAME ] )
179 C<new> is the constuctor for C<IO::Dir> objects. It accepts one optional
180 argument which, if given, C<new> will pass to C<open>
184 The following methods are wrappers for the directory related functions built
185 into perl (the trailing `dir' has been removed from the names). See L<perlfunc>
186 for details of these functions.
190 =item open ( DIRNAME )
204 C<IO::Dir> also provides a interface to reading directories via a tied
205 HASH. The tied HASH extends the interface beyond just the directory
206 reading routines by the use of C<lstat>, from the C<File::stat> package,
207 C<unlink>, C<rmdir> and C<utime>.
211 =item tie %hash, IO::Dir, DIRNAME [, OPTIONS ]
215 The keys of the HASH will be the names of the entries in the directory.
216 Reading a value from the hash will be the result of calling
217 C<File::stat::lstat>. Deleting an element from the hash will call C<unlink>
218 providing that C<DIR_UNLINK> is passed in the C<OPTIONS>.
220 Assigning to an entry in the HASH will cause the time stamps of the file
221 to be modified. If the file does not exist then it will be created. Assigning
222 a single integer to a HASH element will cause both the access and
223 modification times to be changed to that value. Alternatively a reference to
224 an array of two values can be passed. The first array element will be used to
225 set the access time and the second element will be used to set the modification
234 Graham Barr. Currently maintained by the Perl Porters. Please report all
235 bugs to <perl5-porters@perl.org>.
239 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
240 This program is free software; you can redistribute it and/or
241 modify it under the same terms as Perl itself.