This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
readdir() reports file type on VMS, even if zero length.
[perl5.git] / t / op / while_readdir.t
CommitLineData
114c60ec
BG
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
9use strict;
10use warnings;
11
d1c21cec
RGS
12open my $fh, ">", "0" or die "Can't open '0' for writing: $!\n";
13print $fh <<'FILE0';
14This file is here for testing
114c60ec 15
d1c21cec
RGS
16while(readdir $dir){...}
17... while readdir $dir
114c60ec 18
d1c21cec
RGS
19etc
20FILE0
21close $fh;
114c60ec 22
d1c21cec 23plan 10;
114c60ec 24
d1c21cec
RGS
25ok(-f '0', "'0' file is here");
26
27opendir my $dirhandle, '.'
28 or die "Failed test: unable to open directory: $!\n";
29
30my @dir = readdir $dirhandle;
31rewinddir $dirhandle;
114c60ec
BG
32
33{
34 my @list;
35 while(readdir $dirhandle){
36 push @list, $_;
37 }
38 ok( eq_array( \@dir, \@list ), 'while(readdir){push}' );
39 rewinddir $dirhandle;
40}
41
42{
43 my @list;
44 push @list, $_ while readdir $dirhandle;
45 ok( eq_array( \@dir, \@list ), 'push while readdir' );
46 rewinddir $dirhandle;
47}
48
49{
50 my $tmp;
51 my @list;
52 push @list, $tmp while $tmp = readdir $dirhandle;
53 ok( eq_array( \@dir, \@list ), 'push $dir while $dir = readdir' );
54 rewinddir $dirhandle;
55}
56
57{
58 my @list;
59 while( my $dir = readdir $dirhandle){
60 push @list, $dir;
61 }
62 ok( eq_array( \@dir, \@list ), 'while($dir=readdir){push}' );
63 rewinddir $dirhandle;
64}
65
66
67{
68 my @list;
69 my $sub = sub{
70 push @list, $_;
71 };
72 $sub->($_) while readdir $dirhandle;
73 ok( eq_array( \@dir, \@list ), '$sub->($_) while readdir' );
74 rewinddir $dirhandle;
75}
76
d1c21cec
RGS
77{
78 my $works = 0;
79 while(readdir $dirhandle){
df251d1f 80 $_ =~ s/\.$// if defined $_ && $^O eq 'VMS'; # may have zero-length extension
d1c21cec
RGS
81 if( defined $_ && $_ eq '0'){
82 $works = 1;
83 last;
84 }
114c60ec 85 }
d1c21cec
RGS
86 ok( $works, 'while(readdir){} with file named "0"' );
87 rewinddir $dirhandle;
88}
114c60ec 89
d1c21cec
RGS
90{
91 my $works = 0;
92 my $sub = sub{
df251d1f 93 $_ =~ s/\.$// if defined $_ && $^O eq 'VMS'; # may have zero-length extension
d1c21cec
RGS
94 if( defined $_ && $_ eq '0' ){
95 $works = 1;
96 }
97 };
98 $sub->($_) while readdir $dirhandle;
99 ok( $works, '$sub->($_) while readdir; with file named "0"' );
100 rewinddir $dirhandle;
101}
102
103{
104 my $works = 0;
105 while( my $dir = readdir $dirhandle ){
df251d1f 106 $dir =~ s/\.$// if defined $dir && $^O eq 'VMS'; # may have zero-length extension
d1c21cec
RGS
107 if( defined $dir && $dir eq '0'){
108 $works = 1;
109 last;
110 }
114c60ec 111 }
d1c21cec
RGS
112 ok( $works, 'while($dir=readdir){} with file named "0"');
113 rewinddir $dirhandle;
114}
114c60ec 115
d1c21cec
RGS
116{
117 my $tmp;
118 my $ok;
119 my @list;
df251d1f
CB
120 while( $tmp = readdir $dirhandle ){
121 $tmp =~ s/\.$// if defined $tmp && $^O eq 'VMS'; # may have zero-length extension
122 last if defined($tmp)&& !$tmp && ($ok=1)
123 }
d1c21cec
RGS
124 ok( $ok, '$dir while $dir = readdir; with file named "0"' );
125 rewinddir $dirhandle;
114c60ec
BG
126}
127
128closedir $dirhandle;
d1c21cec
RGS
129
130END { 1 while unlink "0" }