diff options
author | Brad Gilbert <b2gills@gmail.com> | 2009-10-22 22:03:40 +0200 |
---|---|---|
committer | Rafael Garcia-Suarez <rgs@consttype.org> | 2009-10-22 22:03:40 +0200 |
commit | 114c60ecb1f775ef1deb4fdc8fb8e3a6f343d13d (patch) | |
tree | 7972b5193369afe8afe15bba2b956ec805ed2cfe /t/op/while_readdir.t | |
parent | d408791feefd619096e6fd7ffe59e868e9359ef8 (diff) | |
download | perl-114c60ecb1f775ef1deb4fdc8fb8e3a6f343d13d.tar.gz |
Bare readdir in while loop now sets $_
Diffstat (limited to 't/op/while_readdir.t')
-rw-r--r-- | t/op/while_readdir.t | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/t/op/while_readdir.t b/t/op/while_readdir.t new file mode 100644 index 0000000000..740bfdd70a --- /dev/null +++ b/t/op/while_readdir.t @@ -0,0 +1,115 @@ +#!./perl + +BEGIN { + chdir 't' if -d 't'; + @INC = '../lib'; + require './test.pl'; +} + +use strict; +use warnings; + +opendir my $dirhandle, '.' or die "Failed test: unable to open directory\n"; + +my @dir = readdir $dirhandle; +rewinddir $dirhandle; + +plan 9; + + + +{ + my @list; + while(readdir $dirhandle){ + push @list, $_; + } + ok( eq_array( \@dir, \@list ), 'while(readdir){push}' ); + rewinddir $dirhandle; +} + +{ + my @list; + push @list, $_ while readdir $dirhandle; + ok( eq_array( \@dir, \@list ), 'push while readdir' ); + rewinddir $dirhandle; +} + +{ + my $tmp; + my @list; + push @list, $tmp while $tmp = readdir $dirhandle; + ok( eq_array( \@dir, \@list ), 'push $dir while $dir = readdir' ); + rewinddir $dirhandle; +} + +{ + my @list; + while( my $dir = readdir $dirhandle){ + push @list, $dir; + } + ok( eq_array( \@dir, \@list ), 'while($dir=readdir){push}' ); + rewinddir $dirhandle; +} + + +{ + my @list; + my $sub = sub{ + push @list, $_; + }; + $sub->($_) while readdir $dirhandle; + ok( eq_array( \@dir, \@list ), '$sub->($_) while readdir' ); + rewinddir $dirhandle; +} + +SKIP:{ + skip ('No file named "0"',4) unless (scalar grep{ defined $_ && $_ eq '0' } @dir ); + + { + my $works = 0; + while(readdir $dirhandle){ + if( defined $_ && $_ eq '0'){ + $works = 1; + last; + } + } + ok( $works, 'while(readdir){} with file named "0"' ); + rewinddir $dirhandle; + } + + { + my $works = 0; + my $sub = sub{ + if( defined $_ && $_ eq '0' ){ + $works = 1; + } + }; + $sub->($_) while readdir $dirhandle; + ok( $works, '$sub->($_) while readdir; with file named "0"' ); + rewinddir $dirhandle; + } + + { + my $works = 0; + while( my $dir = readdir $dirhandle ){ + if( defined $dir && $dir eq '0'){ + $works = 1; + last; + } + } + ok( $works, 'while($dir=readdir){} with file named "0"'); + rewinddir $dirhandle; + } + + { + my $tmp; + my $ok; + my @list; + defined($tmp)&& !$tmp && ($ok=1) while $tmp = readdir $dirhandle; + ok( $ok, '$dir while $dir = readdir; with file named "0"' ); + rewinddir $dirhandle; + } + +} + +closedir $dirhandle; |