# Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
our(@ISA, $VERSION, @EXPORT_OK);
@ISA = qw(Tie::Hash Exporter);
$VERSION = eval $VERSION;
@EXPORT_OK = qw(DIR_UNLINK);
@_ >= 1 && @_ <= 2 or croak
'usage: new IO::Dir [DIRNAME]';
IO
::Dir
::open($dh, $_[0])
@_ == 2 or croak
'usage: $dh->open(DIRNAME)';
unless opendir($dh, $dirname);
# a dir name should always have a ":" in it; assume dirname is
$dirname = ':' . $dirname if ( ($^O
eq 'MacOS') && ($dirname !~ /:/) );
${*$dh}{io_dir_path
} = $dirname;
@_ == 1 or croak
'usage: $dh->close()';
@_ == 1 or croak
'usage: $dh->read()';
@_ == 2 or croak
'usage: $dh->seek(POS)';
@_ == 1 or croak
'usage: $dh->tell()';
@_ == 1 or croak
'usage: $dh->rewind()';
my($class,$dir,$options) = @_;
my $dh = $class->new($dir)
${*$dh}{io_dir_unlink
} = $options & DIR_UNLINK
;
-e File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key);
&lstat(File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key));
my($atime,$mtime) = ref($data) ? @
$data : ($data,$data);
my $file = File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key);
my $io = IO
::File
->new($file,O_CREAT
| O_RDWR
);
utime($atime,$mtime, $file);
# Only unlink if unlink-ing is enabled
unless ${*$dh}{io_dir_unlink
};
my $file = File
::Spec
->catfile(${*$dh}{io_dir_path
}, $key);
IO::Dir - supply object methods for directory handles
while (defined($_ = $d->read)) { something($_); }
while (defined($_ = $d->read)) { something_else($_); }
tie %dir, 'IO::Dir', ".";
print $_, " " , $dir{$_}->size,"\n";
The C<IO::Dir> package provides two interfaces to perl's directory reading
The first interface is an object approach. C<IO::Dir> provides an object
constructor and methods, which are just wrappers around perl's built in
directory reading routines.
=item new ( [ DIRNAME ] )
C<new> is the constructor for C<IO::Dir> objects. It accepts one optional
argument which, if given, C<new> will pass to C<open>
The following methods are wrappers for the directory related functions built
into perl (the trailing `dir' has been removed from the names). See L<perlfunc>
for details of these functions.
C<IO::Dir> also provides an interface to reading directories via a tied
hash. The tied hash extends the interface beyond just the directory
reading routines by the use of C<lstat>, from the C<File::stat> package,
C<unlink>, C<rmdir> and C<utime>.
=item tie %hash, 'IO::Dir', DIRNAME [, OPTIONS ]
The keys of the hash will be the names of the entries in the directory.
Reading a value from the hash will be the result of calling
C<File::stat::lstat>. Deleting an element from the hash will
delete the corresponding file or subdirectory,
provided that C<DIR_UNLINK> is included in the C<OPTIONS>.
Assigning to an entry in the hash will cause the time stamps of the file
to be modified. If the file does not exist then it will be created. Assigning
a single integer to a hash element will cause both the access and
modification times to be changed to that value. Alternatively a reference to
an array of two values can be passed. The first array element will be used to
set the access time and the second element will be used to set the modification
Graham Barr. Currently maintained by the Perl Porters. Please report all
bugs to <perl5-porters@perl.org>.
Copyright (c) 1997-2003 Graham Barr <gbarr@pobox.com>. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.