This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bare readdir in while loop now sets $_
[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
12opendir my $dirhandle, '.' or die "Failed test: unable to open directory\n";
13
14my @dir = readdir $dirhandle;
15rewinddir $dirhandle;
16
17plan 9;
18
19
20
21{
22 my @list;
23 while(readdir $dirhandle){
24 push @list, $_;
25 }
26 ok( eq_array( \@dir, \@list ), 'while(readdir){push}' );
27 rewinddir $dirhandle;
28}
29
30{
31 my @list;
32 push @list, $_ while readdir $dirhandle;
33 ok( eq_array( \@dir, \@list ), 'push while readdir' );
34 rewinddir $dirhandle;
35}
36
37{
38 my $tmp;
39 my @list;
40 push @list, $tmp while $tmp = readdir $dirhandle;
41 ok( eq_array( \@dir, \@list ), 'push $dir while $dir = readdir' );
42 rewinddir $dirhandle;
43}
44
45{
46 my @list;
47 while( my $dir = readdir $dirhandle){
48 push @list, $dir;
49 }
50 ok( eq_array( \@dir, \@list ), 'while($dir=readdir){push}' );
51 rewinddir $dirhandle;
52}
53
54
55{
56 my @list;
57 my $sub = sub{
58 push @list, $_;
59 };
60 $sub->($_) while readdir $dirhandle;
61 ok( eq_array( \@dir, \@list ), '$sub->($_) while readdir' );
62 rewinddir $dirhandle;
63}
64
65SKIP:{
66 skip ('No file named "0"',4) unless (scalar grep{ defined $_ && $_ eq '0' } @dir );
67
68 {
69 my $works = 0;
70 while(readdir $dirhandle){
71 if( defined $_ && $_ eq '0'){
72 $works = 1;
73 last;
74 }
75 }
76 ok( $works, 'while(readdir){} with file named "0"' );
77 rewinddir $dirhandle;
78 }
79
80 {
81 my $works = 0;
82 my $sub = sub{
83 if( defined $_ && $_ eq '0' ){
84 $works = 1;
85 }
86 };
87 $sub->($_) while readdir $dirhandle;
88 ok( $works, '$sub->($_) while readdir; with file named "0"' );
89 rewinddir $dirhandle;
90 }
91
92 {
93 my $works = 0;
94 while( my $dir = readdir $dirhandle ){
95 if( defined $dir && $dir eq '0'){
96 $works = 1;
97 last;
98 }
99 }
100 ok( $works, 'while($dir=readdir){} with file named "0"');
101 rewinddir $dirhandle;
102 }
103
104 {
105 my $tmp;
106 my $ok;
107 my @list;
108 defined($tmp)&& !$tmp && ($ok=1) while $tmp = readdir $dirhandle;
109 ok( $ok, '$dir while $dir = readdir; with file named "0"' );
110 rewinddir $dirhandle;
111 }
112
113}
114
115closedir $dirhandle;