<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># IO::Dir.pm
#
# Copyright (c) 1997-8 Graham Barr &lt;gbarr@pobox.com&gt;. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

package IO::Dir;

use 5.008_001;

use strict;
use Carp;
use Symbol;
use Exporter;
use IO::File;
use Tie::Hash;
use File::stat;
use File::Spec;

our @ISA = qw(Tie::Hash Exporter);
our $VERSION = "1.49";

our @EXPORT_OK = qw(DIR_UNLINK);

sub DIR_UNLINK () { 1 }

sub new {
    @_ &gt;= 1 &amp;&amp; @_ &lt;= 2 or croak 'usage: IO::Dir-&gt;new([DIRNAME])';
    my $class = shift;
    my $dh = gensym;
    if (@_) {
	IO::Dir::open($dh, $_[0])
	    or return undef;
    }
    bless $dh, $class;
}

sub DESTROY {
    my ($dh) = @_;
    local($., $@, $!, $^E, $?);
    no warnings 'io';
    closedir($dh);
}

sub open {
    @_ == 2 or croak 'usage: $dh-&gt;open(DIRNAME)';
    my ($dh, $dirname) = @_;
    return undef
	unless opendir($dh, $dirname);
    # a dir name should always have a ":" in it; assume dirname is
    # in current directory
    $dirname = ':' .  $dirname if ( ($^O eq 'MacOS') &amp;&amp; ($dirname !~ /:/) );
    ${*$dh}{io_dir_path} = $dirname;
    1;
}

sub close {
    @_ == 1 or croak 'usage: $dh-&gt;close()';
    my ($dh) = @_;
    closedir($dh);
}

sub read {
    @_ == 1 or croak 'usage: $dh-&gt;read()';
    my ($dh) = @_;
    readdir($dh);
}

sub seek {
    @_ == 2 or croak 'usage: $dh-&gt;seek(POS)';
    my ($dh,$pos) = @_;
    seekdir($dh,$pos);
}

sub tell {
    @_ == 1 or croak 'usage: $dh-&gt;tell()';
    my ($dh) = @_;
    telldir($dh);
}

sub rewind {
    @_ == 1 or croak 'usage: $dh-&gt;rewind()';
    my ($dh) = @_;
    rewinddir($dh);
}

sub TIEHASH {
    my($class,$dir,$options) = @_;

    my $dh = $class-&gt;new($dir)
	or return undef;

    $options ||= 0;

    ${*$dh}{io_dir_unlink} = $options &amp; DIR_UNLINK;
    $dh;
}

sub FIRSTKEY {
    my($dh) = @_;
    $dh-&gt;rewind;
    scalar $dh-&gt;read;
}

sub NEXTKEY {
    my($dh) = @_;
    scalar $dh-&gt;read;
}

sub EXISTS {
    my($dh,$key) = @_;
    -e File::Spec-&gt;catfile(${*$dh}{io_dir_path}, $key);
}

sub FETCH {
    my($dh,$key) = @_;
    &amp;lstat(File::Spec-&gt;catfile(${*$dh}{io_dir_path}, $key));
}

sub STORE {
    my($dh,$key,$data) = @_;
    my($atime,$mtime) = ref($data) ? @$data : ($data,$data);
    my $file = File::Spec-&gt;catfile(${*$dh}{io_dir_path}, $key);
    unless(-e $file) {
	my $io = IO::File-&gt;new($file,O_CREAT | O_RDWR);
	$io-&gt;close if $io;
    }
    utime($atime,$mtime, $file);
}

sub DELETE {
    my($dh,$key) = @_;

    # Only unlink if unlink-ing is enabled
    return 0
	unless ${*$dh}{io_dir_unlink};

    my $file = File::Spec-&gt;catfile(${*$dh}{io_dir_path}, $key);

    -d $file
	? rmdir($file)
	: unlink($file);
}

1;

__END__

=head1 NAME 

IO::Dir - supply object methods for directory handles

=head1 SYNOPSIS

    use IO::Dir;
    $d = IO::Dir-&gt;new(".");
    if (defined $d) {
        while (defined($_ = $d-&gt;read)) { something($_); }
        $d-&gt;rewind;
        while (defined($_ = $d-&gt;read)) { something_else($_); }
        undef $d;
    }

    tie %dir, 'IO::Dir', ".";
    foreach (keys %dir) {
	print $_, " " , $dir{$_}-&gt;size,"\n";
    }

=head1 DESCRIPTION

The C&lt;IO::Dir&gt; package provides two interfaces to perl's directory reading
routines.

The first interface is an object approach. C&lt;IO::Dir&gt; provides an object
constructor and methods, which are just wrappers around perl's built in
directory reading routines.

=over 4

=item new ( [ DIRNAME ] )

C&lt;new&gt; is the constructor for C&lt;IO::Dir&gt; objects. It accepts one optional
argument which,  if given, C&lt;new&gt; will pass to C&lt;open&gt;

=back

The following methods are wrappers for the directory related functions built
into perl (the trailing 'dir' has been removed from the names). See L&lt;perlfunc&gt;
for details of these functions.

=over 4

=item open ( DIRNAME )

=item read ()

=item seek ( POS )

=item tell ()

=item rewind ()

=item close ()

=back

C&lt;IO::Dir&gt; 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&lt;lstat&gt;, from the C&lt;File::stat&gt; package,
C&lt;unlink&gt;, C&lt;rmdir&gt; and C&lt;utime&gt;.

=over 4

=item tie %hash, 'IO::Dir', DIRNAME [, OPTIONS ]

=back

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&lt;File::stat::lstat&gt;.  Deleting an element from the hash will 
delete the corresponding file or subdirectory,
provided that C&lt;DIR_UNLINK&gt; is included in the C&lt;OPTIONS&gt;.

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
time.

=head1 SEE ALSO

L&lt;File::stat&gt;

=head1 AUTHOR

Graham Barr. Currently maintained by the Perl Porters.  Please report all
bugs at L&lt;https://github.com/Perl/perl5/issues&gt;.

=head1 COPYRIGHT

Copyright (c) 1997-2003 Graham Barr &lt;gbarr@pobox.com&gt;. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut
</pre></body></html>