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 $VERSION = eval $VERSION;
24 @EXPORT_OK = qw(DIR_UNLINK);
26 sub DIR_UNLINK () { 1 }
29 @_ >= 1 && @_ <= 2 or croak 'usage: new IO::Dir [DIRNAME]';
33 IO::Dir::open($dh, $_[0])
41 local($., $@, $!, $^E, $?);
47 @_ == 2 or croak 'usage: $dh->open(DIRNAME)';
48 my ($dh, $dirname) = @_;
50 unless opendir($dh, $dirname);
51 # a dir name should always have a ":" in it; assume dirname is
52 # in current directory
53 $dirname = ':' . $dirname if ( ($^O eq 'MacOS') && ($dirname !~ /:/) );
54 ${*$dh}{io_dir_path} = $dirname;
59 @_ == 1 or croak 'usage: $dh->close()';
65 @_ == 1 or croak 'usage: $dh->read()';
71 @_ == 2 or croak 'usage: $dh->seek(POS)';
77 @_ == 1 or croak 'usage: $dh->tell()';
83 @_ == 1 or croak 'usage: $dh->rewind()';
89 my($class,$dir,$options) = @_;
91 my $dh = $class->new($dir)
96 ${*$dh}{io_dir_unlink} = $options & DIR_UNLINK;
113 -e File::Spec->catfile(${*$dh}{io_dir_path}, $key);
118 &lstat(File::Spec->catfile(${*$dh}{io_dir_path}, $key));
122 my($dh,$key,$data) = @_;
123 my($atime,$mtime) = ref($data) ? @$data : ($data,$data);
124 my $file = File::Spec->catfile(${*$dh}{io_dir_path}, $key);
126 my $io = IO::File->new($file,O_CREAT | O_RDWR);
129 utime($atime,$mtime, $file);
135 # Only unlink if unlink-ing is enabled
137 unless ${*$dh}{io_dir_unlink};
139 my $file = File::Spec->catfile(${*$dh}{io_dir_path}, $key);
152 IO::Dir - supply object methods for directory handles
157 $d = IO::Dir->new(".");
159 while (defined($_ = $d->read)) { something($_); }
161 while (defined($_ = $d->read)) { something_else($_); }
165 tie %dir, 'IO::Dir', ".";
166 foreach (keys %dir) {
167 print $_, " " , $dir{$_}->size,"\n";
172 The C<IO::Dir> package provides two interfaces to perl's directory reading
175 The first interface is an object approach. C<IO::Dir> provides an object
176 constructor and methods, which are just wrappers around perl's built in
177 directory reading routines.
181 =item new ( [ DIRNAME ] )
183 C<new> is the constructor for C<IO::Dir> objects. It accepts one optional
184 argument which, if given, C<new> will pass to C<open>
188 The following methods are wrappers for the directory related functions built
189 into perl (the trailing `dir' has been removed from the names). See L<perlfunc>
190 for details of these functions.
194 =item open ( DIRNAME )
208 C<IO::Dir> also provides an interface to reading directories via a tied
209 hash. The tied hash extends the interface beyond just the directory
210 reading routines by the use of C<lstat>, from the C<File::stat> package,
211 C<unlink>, C<rmdir> and C<utime>.
215 =item tie %hash, 'IO::Dir', DIRNAME [, OPTIONS ]
219 The keys of the hash will be the names of the entries in the directory.
220 Reading a value from the hash will be the result of calling
221 C<File::stat::lstat>. Deleting an element from the hash will
222 delete the corresponding file or subdirectory,
223 provided that C<DIR_UNLINK> is included in the C<OPTIONS>.
225 Assigning to an entry in the hash will cause the time stamps of the file
226 to be modified. If the file does not exist then it will be created. Assigning
227 a single integer to a hash element will cause both the access and
228 modification times to be changed to that value. Alternatively a reference to
229 an array of two values can be passed. The first array element will be used to
230 set the access time and the second element will be used to set the modification
239 Graham Barr. Currently maintained by the Perl Porters. Please report all
240 bugs to <perl5-porters@perl.org>.
244 Copyright (c) 1997-2003 Graham Barr <gbarr@pobox.com>. All rights reserved.
245 This program is free software; you can redistribute it and/or
246 modify it under the same terms as Perl itself.