Skip Menu | You are currently an anonymous guest. | Login | Return to Main | About rt.cpan.org
 

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.

X Report information
Id: 29374
Status: resolved
Left: 0 min
Priority: 0/0
Queue: Path-Class

Owner: Nobody
Requestors: DLO <wilburlo [...] gmail.com>
Cc:
AdminCc:

Severity: Important
Broken in: (no value)
Fixed in: (no value)



X History Display mode: Brief headersFull headers
#   Fri Sep 14 14:47:05 2007 DLO - Ticket created  
Subject: Documentation shows a bad example.
[text/plain 562b]
In the Path::Class::Dir documentation you have the following example

(Last few lines in SYNOPSIS & $dir->next documentation)

# Iterate with Path::Class methods:
while (my $file = $dir->next) {
# $file is a Path::Class::File or Path::Class::Dir object
...
}

If there is a directory named "0" (zero), then that will prematurely
terminate the while loop, since $file will be 0. (which is false).

There probably should be a $dir->eof() (or something else of that name)
subroutine.

while ( ! $dir->eof() ) {
my $file = $dir->next();
}

-daniel

#   Fri Sep 14 14:54:19 2007 DLO - Correspondence added  
Subject: children subroutine is also bugged.
[text/plain 818b]
Here is a test case that breaks the $dir->children()

[11:52am]/home/daniel/t>cat go.pl
use Path::Class;

my $dir = Path::Class::dir('.');
print join ', ', $dir->children();
print "\n";

[11:52am]/home/daniel/t>ls -al
total 6
drwxr-xr-x 2 daniel staff 512 Sep 14 11:51 .
drwxr-xr-x 8 daniel staff 512 Sep 14 11:49 ..
-rw-r--r-- 1 daniel staff 0 Sep 14 11:51 0
-rw-r--r-- 1 daniel staff 0 Sep 14 11:51 1
-rw-r--r-- 1 daniel staff 0 Sep 14 11:51 2
-rw-r--r-- 1 daniel staff 0 Sep 14 11:51 3
-rw-r--r-- 1 daniel staff 0 Sep 14 11:51 4
-rw-r--r-- 1 daniel staff 0 Sep 14 11:51 5
-rw-r--r-- 1 daniel staff 99 Sep 14 11:51 go.pl
[11:52am]/home/daniel/t>perl go.pl
./go.pl, ./1, ./2, ./3
[11:52am]/home/daniel/t>


files are created by
touch 1
touch 2
touch 3
touch 0
touch 4
touch 5


#   Fri Sep 14 14:54:22 2007 DLO - Status changed from 'new' to 'open'  
#   Sun Sep 16 21:17:16 2007 KWILLIAMS - Correspondence added  
From: KWILLIAMS[...]cpan.org
[text/plain 100b]
Actually $file won't be "0" it'll be "$dir/0" or the platform equivalent, which is non-false.

-Ken
#   Sun Sep 16 21:20:44 2007 KWILLIAMS - Correspondence added  
[text/plain 483b]
Ah, I see I am wrong in the case where a *directory* is called "0" and $dir is the current
directory. I think this is an actual bug in the code, because it should be stringifying to "./0",
not merely "0" to be consistent with files:

[/tmp] % perl -MPath::Class -le '$dir=dir("."); while (my $file = $dir->next) { print $file }'
.
..
./0
501
./cs_cache_lock_92
hsperfdata_ken
./objc_sharing_ppc_26
./objc_sharing_ppc_4294967294
./objc_sharing_ppc_501
./objc_sharing_ppc_92


-Ken
#   Mon Sep 17 00:17:26 2007 DLO - Correspondence added  
From: daniel_lo[...]picturetrail.com
[text/plain 897b]
I need to include test cases for my own thoughts :).

The problem is in the while loop condition check, the return scalar
value of the IO::Dir->read (readdir). The code exits before Path::Class
has a chance to stringify it.

The while loop receives a scalar value from $handle (IO::Dir), which in
the case of a file or directory that is a name of "0" (zero) it
evaluates to false and the loop is terminated.

I've included a test case below:

use Path::Class;
use Path::Class::Dir;

mkdir '1';
mkdir '0';

my $dir=dir('.');

my $handle = $dir->open;

# while ( defined ( my $file = $handle->read ) ) { # this works.
while ( my $file = $handle->read ) { # IO::Dir->read returns a scalar value
print "1: ", $file, "\n";
$file = $dir->file($file); # Turn $file into Path::Class::File object
print "2: ", $file, "\n";
print "\n";
}

# clean up after ourself.
rmdir '1';
rmdir '0';

#   Sun Mar 30 13:06:09 2008 KWILLIAMS - Correspondence added  
[text/plain 172b]
Hi Daniel,

This problem has been fixed for the next release, the idiom 'while($x = $dir->next) {...}' can
now safely be used without risk of early termination.

-Ken


#   Sun Mar 30 13:06:11 2008 KWILLIAMS - Status changed from 'open' to 'resolved'