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
|
|
19346
|
use 5.008_001; |
|
2
|
|
|
|
|
5
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
16
|
use strict; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
32
|
|
12
|
2
|
|
|
2
|
|
8
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
78
|
|
13
|
2
|
|
|
2
|
|
9
|
use Symbol; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
83
|
|
14
|
2
|
|
|
2
|
|
10
|
use Exporter; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
57
|
|
15
|
2
|
|
|
2
|
|
359
|
use IO::File; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
210
|
|
16
|
2
|
|
|
2
|
|
849
|
use Tie::Hash; |
|
2
|
|
|
|
|
1517
|
|
|
2
|
|
|
|
|
51
|
|
17
|
2
|
|
|
2
|
|
812
|
use File::stat; |
|
2
|
|
|
|
|
11415
|
|
|
2
|
|
|
|
|
7
|
|
18
|
2
|
|
|
2
|
|
95
|
use File::Spec; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
292
|
|
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
|
3714
|
@_ >= 1 && @_ <= 2 or croak 'usage: IO::Dir->new([DIRNAME])'; |
29
|
3
|
|
|
|
|
13
|
my $class = shift; |
30
|
3
|
|
|
|
|
16
|
my $dh = gensym; |
31
|
3
|
50
|
|
|
|
65
|
if (@_) { |
32
|
3
|
50
|
|
|
|
12
|
IO::Dir::open($dh, $_[0]) |
33
|
|
|
|
|
|
|
or return undef; |
34
|
|
|
|
|
|
|
} |
35
|
3
|
|
|
|
|
22
|
bless $dh, $class; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub DESTROY { |
39
|
3
|
|
|
3
|
|
108
|
my ($dh) = @_; |
40
|
3
|
|
|
|
|
36
|
local($., $@, $!, $^E, $?); |
41
|
2
|
|
|
2
|
|
12
|
no warnings 'io'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1428
|
|
42
|
3
|
|
|
|
|
173
|
closedir($dh); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub open { |
46
|
3
|
50
|
|
3
|
1
|
14
|
@_ == 2 or croak 'usage: $dh->open(DIRNAME)'; |
47
|
3
|
|
|
|
|
5
|
my ($dh, $dirname) = @_; |
48
|
|
|
|
|
|
|
return undef |
49
|
3
|
50
|
|
|
|
91
|
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
|
|
|
29
|
$dirname = ':' . $dirname if ( ($^O eq 'MacOS') && ($dirname !~ /:/) ); |
53
|
3
|
|
|
|
|
8
|
${*$dh}{io_dir_path} = $dirname; |
|
3
|
|
|
|
|
18
|
|
54
|
3
|
|
|
|
|
14
|
1; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub close { |
58
|
1
|
50
|
|
1
|
1
|
62
|
@_ == 1 or croak 'usage: $dh->close()'; |
59
|
1
|
|
|
|
|
2
|
my ($dh) = @_; |
60
|
1
|
|
|
|
|
15
|
closedir($dh); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub read { |
64
|
18
|
50
|
|
18
|
1
|
367
|
@_ == 1 or croak 'usage: $dh->read()'; |
65
|
18
|
|
|
|
|
33
|
my ($dh) = @_; |
66
|
18
|
|
|
|
|
133
|
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
|
76
|
@_ == 1 or croak 'usage: $dh->rewind()'; |
83
|
3
|
|
|
|
|
8
|
my ($dh) = @_; |
84
|
3
|
|
|
|
|
28
|
rewinddir($dh); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub TIEHASH { |
88
|
2
|
|
|
2
|
|
167
|
my($class,$dir,$options) = @_; |
89
|
|
|
|
|
|
|
|
90
|
2
|
50
|
|
|
|
6
|
my $dh = $class->new($dir) |
91
|
|
|
|
|
|
|
or return undef; |
92
|
|
|
|
|
|
|
|
93
|
2
|
|
100
|
|
|
11
|
$options ||= 0; |
94
|
|
|
|
|
|
|
|
95
|
2
|
|
|
|
|
7
|
${*$dh}{io_dir_unlink} = $options & DIR_UNLINK; |
|
2
|
|
|
|
|
9
|
|
96
|
2
|
|
|
|
|
11
|
$dh; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub FIRSTKEY { |
100
|
1
|
|
|
1
|
|
12
|
my($dh) = @_; |
101
|
1
|
|
|
|
|
3
|
$dh->rewind; |
102
|
1
|
|
|
|
|
3
|
scalar $dh->read; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub NEXTKEY { |
106
|
13
|
|
|
13
|
|
19
|
my($dh) = @_; |
107
|
13
|
|
|
|
|
34
|
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
|
|
52
|
my($dh,$key) = @_; |
117
|
2
|
|
|
|
|
6
|
&lstat(File::Spec->catfile(${*$dh}{io_dir_path}, $key)); |
|
2
|
|
|
|
|
61
|
|
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
|
|
594
|
my($dh,$key) = @_; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Only unlink if unlink-ing is enabled |
135
|
|
|
|
|
|
|
return 0 |
136
|
2
|
100
|
|
|
|
24
|
unless ${*$dh}{io_dir_unlink}; |
|
2
|
|
|
|
|
12
|
|
137
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
3
|
my $file = File::Spec->catfile(${*$dh}{io_dir_path}, $key); |
|
1
|
|
|
|
|
8
|
|
139
|
|
|
|
|
|
|
|
140
|
1
|
50
|
|
|
|
68
|
-d $file |
141
|
|
|
|
|
|
|
? rmdir($file) |
142
|
|
|
|
|
|
|
: unlink($file); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
__END__ |