Subject: | Method for Distinguishing Files and Directories |
The Path::Class::Dir doc has this in an example:
while (my $file = $dir->next) {
next unless -f $file;
That's something I'm doing in a script, and the -f test irks me: $file already knows whether it's a file or a directory, yet I'm making Perl go to all the bother of looking on the filesystem again to find out.
One way of avoiding the filesystem hit would be:
next unless -f _;
But that's just horrid! (And breaks encapsulation.) So what I'm actually doing is asking $file what it is:
next unless $file->isa('Path::Class::File');
That works fine. But I was wondering whether having a Path::Class method explicitly returning the type would be good:
next unless $file->type eq 'file';
That doesn't seem like much of a saving over checking with ->isa, but I think it's significantly more readable, and avoids giving the impression of being overly concerned with the internal representation.
What do you think? I'd be happy to write the patch, docs, and tests for this (and my other recent requests) if you'd be willing to accept such features into the distribution.
Cheers.
Smylers