| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # IO::Dir.pm | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # Copyright (c) 1997-8 Graham Barr . 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. | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | package IO::Dir; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 2 |  |  | 2 |  | 18260 | use 5.008_001; | 
|  | 2 |  |  |  |  | 7 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 2 |  |  | 2 |  | 16 | use strict; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 35 |  | 
| 12 | 2 |  |  | 2 |  | 9 | use Carp; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 96 |  | 
| 13 | 2 |  |  | 2 |  | 10 | use Symbol; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 138 |  | 
| 14 | 2 |  |  | 2 |  | 10 | use Exporter; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 60 |  | 
| 15 | 2 |  |  | 2 |  | 381 | use IO::File; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 229 |  | 
| 16 | 2 |  |  | 2 |  | 861 | use Tie::Hash; | 
|  | 2 |  |  |  |  | 1619 |  | 
|  | 2 |  |  |  |  | 56 |  | 
| 17 | 2 |  |  | 2 |  | 840 | use File::stat; | 
|  | 2 |  |  |  |  | 12351 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 18 | 2 |  |  | 2 |  | 115 | use File::Spec; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 334 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | our @ISA = qw(Tie::Hash Exporter); | 
| 21 |  |  |  |  |  |  | our $VERSION = "1.49"; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | our @EXPORT_OK = qw(DIR_UNLINK); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub DIR_UNLINK () { 1 } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub new { | 
| 28 | 3 | 50 | 33 | 3 | 1 | 3785 | @_ >= 1 && @_ <= 2 or croak 'usage: IO::Dir->new([DIRNAME])'; | 
| 29 | 3 |  |  |  |  | 10 | my $class = shift; | 
| 30 | 3 |  |  |  |  | 18 | my $dh = gensym; | 
| 31 | 3 | 50 |  |  |  | 65 | if (@_) { | 
| 32 | 3 | 50 |  |  |  | 16 | IO::Dir::open($dh, $_[0]) | 
| 33 |  |  |  |  |  |  | or return undef; | 
| 34 |  |  |  |  |  |  | } | 
| 35 | 3 |  |  |  |  | 18 | bless $dh, $class; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub DESTROY { | 
| 39 | 3 |  |  | 3 |  | 70 | my ($dh) = @_; | 
| 40 | 3 |  |  |  |  | 34 | local($., $@, $!, $^E, $?); | 
| 41 | 2 |  |  | 2 |  | 12 | no warnings 'io'; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 1474 |  | 
| 42 | 3 |  |  |  |  | 158 | closedir($dh); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub open { | 
| 46 | 3 | 50 |  | 3 | 1 | 13 | @_ == 2 or croak 'usage: $dh->open(DIRNAME)'; | 
| 47 | 3 |  |  |  |  | 8 | my ($dh, $dirname) = @_; | 
| 48 |  |  |  |  |  |  | return undef | 
| 49 | 3 | 50 |  |  |  | 76 | unless opendir($dh, $dirname); | 
| 50 |  |  |  |  |  |  | # a dir name should always have a ":" in it; assume dirname is | 
| 51 |  |  |  |  |  |  | # in current directory | 
| 52 | 3 | 50 | 33 |  |  | 24 | $dirname = ':' .  $dirname if ( ($^O eq 'MacOS') && ($dirname !~ /:/) ); | 
| 53 | 3 |  |  |  |  | 7 | ${*$dh}{io_dir_path} = $dirname; | 
|  | 3 |  |  |  |  | 17 |  | 
| 54 | 3 |  |  |  |  | 20 | 1; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub close { | 
| 58 | 1 | 50 |  | 1 | 1 | 57 | @_ == 1 or croak 'usage: $dh->close()'; | 
| 59 | 1 |  |  |  |  | 2 | my ($dh) = @_; | 
| 60 | 1 |  |  |  |  | 17 | closedir($dh); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub read { | 
| 64 | 18 | 50 |  | 18 | 1 | 345 | @_ == 1 or croak 'usage: $dh->read()'; | 
| 65 | 18 |  |  |  |  | 24 | my ($dh) = @_; | 
| 66 | 18 |  |  |  |  | 134 | readdir($dh); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub seek { | 
| 70 | 0 | 0 |  | 0 | 1 | 0 | @_ == 2 or croak 'usage: $dh->seek(POS)'; | 
| 71 | 0 |  |  |  |  | 0 | my ($dh,$pos) = @_; | 
| 72 | 0 |  |  |  |  | 0 | seekdir($dh,$pos); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub tell { | 
| 76 | 0 | 0 |  | 0 | 1 | 0 | @_ == 1 or croak 'usage: $dh->tell()'; | 
| 77 | 0 |  |  |  |  | 0 | my ($dh) = @_; | 
| 78 | 0 |  |  |  |  | 0 | telldir($dh); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub rewind { | 
| 82 | 3 | 50 |  | 3 | 1 | 69 | @_ == 1 or croak 'usage: $dh->rewind()'; | 
| 83 | 3 |  |  |  |  | 5 | my ($dh) = @_; | 
| 84 | 3 |  |  |  |  | 20 | rewinddir($dh); | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub TIEHASH { | 
| 88 | 2 |  |  | 2 |  | 180 | my($class,$dir,$options) = @_; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 2 | 50 |  |  |  | 6 | my $dh = $class->new($dir) | 
| 91 |  |  |  |  |  |  | or return undef; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 2 |  | 100 |  |  | 10 | $options ||= 0; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 2 |  |  |  |  | 3 | ${*$dh}{io_dir_unlink} = $options & DIR_UNLINK; | 
|  | 2 |  |  |  |  | 8 |  | 
| 96 | 2 |  |  |  |  | 13 | $dh; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub FIRSTKEY { | 
| 100 | 1 |  |  | 1 |  | 12 | my($dh) = @_; | 
| 101 | 1 |  |  |  |  | 2 | $dh->rewind; | 
| 102 | 1 |  |  |  |  | 3 | scalar $dh->read; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub NEXTKEY { | 
| 106 | 13 |  |  | 13 |  | 19 | my($dh) = @_; | 
| 107 | 13 |  |  |  |  | 33 | scalar $dh->read; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub EXISTS { | 
| 111 | 0 |  |  | 0 |  | 0 | my($dh,$key) = @_; | 
| 112 | 0 |  |  |  |  | 0 | -e File::Spec->catfile(${*$dh}{io_dir_path}, $key); | 
|  | 0 |  |  |  |  | 0 |  | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub FETCH { | 
| 116 | 2 |  |  | 2 |  | 47 | my($dh,$key) = @_; | 
| 117 | 2 |  |  |  |  | 4 | &lstat(File::Spec->catfile(${*$dh}{io_dir_path}, $key)); | 
|  | 2 |  |  |  |  | 49 |  | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | sub STORE { | 
| 121 | 0 |  |  | 0 |  | 0 | my($dh,$key,$data) = @_; | 
| 122 | 0 | 0 |  |  |  | 0 | my($atime,$mtime) = ref($data) ? @$data : ($data,$data); | 
| 123 | 0 |  |  |  |  | 0 | my $file = File::Spec->catfile(${*$dh}{io_dir_path}, $key); | 
|  | 0 |  |  |  |  | 0 |  | 
| 124 | 0 | 0 |  |  |  | 0 | unless(-e $file) { | 
| 125 | 0 |  |  |  |  | 0 | my $io = IO::File->new($file,O_CREAT | O_RDWR); | 
| 126 | 0 | 0 |  |  |  | 0 | $io->close if $io; | 
| 127 |  |  |  |  |  |  | } | 
| 128 | 0 |  |  |  |  | 0 | utime($atime,$mtime, $file); | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | sub DELETE { | 
| 132 | 2 |  |  | 2 |  | 576 | my($dh,$key) = @_; | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | # Only unlink if unlink-ing is enabled | 
| 135 |  |  |  |  |  |  | return 0 | 
| 136 | 2 | 100 |  |  |  | 28 | unless ${*$dh}{io_dir_unlink}; | 
|  | 2 |  |  |  |  | 12 |  | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 1 |  |  |  |  | 2 | my $file = File::Spec->catfile(${*$dh}{io_dir_path}, $key); | 
|  | 1 |  |  |  |  | 8 |  | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 1 | 50 |  |  |  | 66 | -d $file | 
| 141 |  |  |  |  |  |  | ? rmdir($file) | 
| 142 |  |  |  |  |  |  | : unlink($file); | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | 1; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | __END__ |