Commits

shl...@6f433eb9-a002-0410-945b-ee5462c261ee  committed 12e5f3e

Added the ->is_file() method to ::Result.

  • Participants
  • Parent commits d0b9d9f

Comments (0)

Files changed (4)

     the $dir_str explicitly. This reduced the code considerably, and
     eliminated a similar symptom to this one:
         - http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm
+    - Added the ->is_file() method to ::Result.
 
 0.2.0 - Sun Feb 22 11:52:06 IST 2009
     - Optimization: now not checking for the existence of the callback() after 

File lib/File/Find/Object.pm

         dir_components => \@comps,
         base => shift(@comps),
         stat_ret => scalar($self->_top_stat_copy()),
+        is_file => scalar(-f _),
     };
 
     if ($self->_curr_not_a_dir())

File lib/File/Find/Object/Result.pm

         (map { $_ => $_ } (qw(
         base
         basename
+        is_file
         path
         dir_components
         stat_ret
 
 Returns true if the result refers to a directory.
 
+=head2 $result->is_file()
+
+Returns true if the result refers to a plain file (equivalent to the Perl
+C<-f> operator).
+
 =head2 $result->dir_components()
 
 The components of the directory part of the path starting from base() 

File t/03traverse.t

 use strict;
 use warnings;
 
-use Test::More tests => 43;
+use Test::More tests => 44;
 
 BEGIN
 {
 
     rmtree($t->get_path("./t/sample-data/traverse-1"));
 }
+
+{
+    my $tree =
+    {
+        'name' => "traverse-1/",
+        'subs' =>
+        [
+            {
+                'name' => "b.doc",
+                'contents' => "This file was spotted in the wild.",
+            },            
+            {
+                'name' => "a/",
+            },
+            {
+                'name' => "foo/",
+                'subs' =>
+                [
+                    {
+                        'name' => "file.txt",
+                        'contents' => "A file that should come before yet/",
+                    },
+                    {
+                        'name' => "yet/",
+                    },
+                ],
+            },
+        ],
+    };
+
+    my $t = File::Find::Object::TreeCreate->new();
+    $t->create_tree("./t/sample-data/", $tree);
+    my $ff = 
+        File::Find::Object->new(
+            {},
+            $t->get_path("./t/sample-data/traverse-1")
+        );
+
+    my @results;
+
+    while (my $r = $ff->next_obj())
+    {
+        if ($r->is_file())
+        {
+            push @results, $r->path();
+        }
+    }
+
+    # TEST
+    is_deeply(
+        \@results,
+        [
+            map { $t->get_path("t/sample-data/traverse-1/$_") }
+            (qw(b.doc foo/file.txt))
+        ],
+        "Checking for regular, lexicographically sorted order",
+    );
+
+    rmtree($t->get_path("./t/sample-data/traverse-1"))
+}