This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In buildtoc, move the "has a NAME?" file check into podset().
authorNicholas Clark <nick@ccl4.org>
Sun, 18 Dec 2011 16:21:14 +0000 (17:21 +0100)
committerNicholas Clark <nick@ccl4.org>
Mon, 19 Dec 2011 12:55:19 +0000 (13:55 +0100)
Instead of opening each file to search for /^=head1\s+NAME\b/ within the
File::Find::find() callback, move the check to the main processing code in
podset(), where the file is already open. Seeking the file back to the start
is less costly than closing and subsequently reopening, and performing all the
reads at the same time reduces pressure on the OS disk cache.

pod/buildtoc

index c61a425..e35dcc4 100644 (file)
@@ -34,17 +34,7 @@ find(sub {
       return if $file =~ m!XS/(?:APItest|Typemap)!;
       my $pod = $_;
       return if $pod =~ s/pm$/pod/ && -e $pod;
-      open my $f, '<', $_ or my_die "Can't open file '$_': $!";
-      {
-       my $line;
-       while ($line = <$f>) {
-         if ($line =~ /^=head1\s+NAME\b/) {
-           push @modpods, $file;
-           return;
-         }
-       }
-       warn "$0: NOTE: cannot find '=head1 NAME' in:\n  $file\n" unless $Quiet;
-      }
+      push @modpods, $file;
     }
   }, 'lib');
 
@@ -222,10 +212,24 @@ my ($inhead1, $inhead2, $initem);
 sub podset {
     my ($pod, $file) = @_;
 
-    local $/ = '';
+    open my $fh, '<', $file or my_die "Can't open file '$file' for $pod: $!";
+
     local *_;
+    my $found_pod;
+    while (<$fh>) {
+        if (/^=head1\s+NAME\b/) {
+            ++$found_pod;
+            last;
+        }
+    }
 
-    open my $fh, '<', $file or my_die "Can't open file '$file' for $pod: $!";
+    unless ($found_pod) {
+       warn "$0: NOTE: cannot find '=head1 NAME' in:\n  $file\n" unless $Quiet;
+        return;
+    }
+
+    seek $fh, 0, 0 or my_die "Can't rewind file '$file': $!";
+    local $/ = '';
 
     while(<$fh>) {
        tr/\015//d;