This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In buildtoc, eliminate a substitution which mangles nested package names.
[perl5.git] / pod / buildtoc
index 4054fda..1a4e02c 100644 (file)
@@ -1,41 +1,58 @@
 #!/usr/bin/perl -w
 
 use strict;
-use vars qw($masterpodfile %Build %Targets $Verbose $Up %Ignore
+use vars qw($masterpodfile %Build %Targets $Verbose $Quiet %Ignore
            @Master %Readmes %Pods %Aux %Readmepods %Pragmata %Modules
-           %Copies);
+           %Copies %Generated $Test);
 use File::Spec;
 use File::Find;
 use FindBin;
 use Text::Tabs;
 use Text::Wrap;
 use Getopt::Long;
+use Carp;
 
 no locale;
 
-$Up = File::Spec->updir;
-$masterpodfile = File::Spec->catdir($Up, "pod.lst");
+{
+  my $Top = File::Spec->catdir($FindBin::Bin, File::Spec->updir);
+
+  sub abs_from_top {
+    my $path = shift;
+    return File::Spec->catdir($Top, split /\//, $path) if $path =~ s!/\z!!;
+    return File::Spec->catfile($Top, split /\//, $path);
+  }
+}
+
+$masterpodfile = abs_from_top('pod.lst');
 
 # Generate any/all of these files
 # --verbose gives slightly more output
+# --quiet suppresses routine warnings
 # --build-all tries to build everything
 # --build-foo updates foo as follows
 # --showfiles shows the files to be changed
+# --test exit early, exit if perl.pod, pod.lst, MANIFEST are
+#   consistent, die otherwise.
 
 %Targets
   = (
-     toc => "perltoc.pod",
-     manifest => File::Spec->catdir($Up, "MANIFEST"),
-     perlpod => "perl.pod",
-     vms => File::Spec->catdir($Up, "vms", "descrip_mms.template"),
-     nmake => File::Spec->catdir($Up, "win32", "Makefile"),
-     dmake => File::Spec->catdir($Up, "win32", "makefile.mk"),
-     podmak => File::Spec->catdir($Up, "win32", "pod.mak"),
-     # plan9 =>  File::Spec->catdir($Up, "plan9", "mkfile"),
-     unix => File::Spec->catdir($Up, "Makefile.SH"),
+     toc => 'pod/perltoc.pod',
+     manifest => 'MANIFEST',
+     perlpod => 'pod/perl.pod',
+     vms => 'vms/descrip_mms.template',
+     nmake => 'win32/Makefile',
+     dmake => 'win32/makefile.mk',
+     podmak => 'win32/pod.mak',
+     # plan9 =>  'plan9/mkfile'),
+     unix => 'Makefile.SH',
      # TODO: add roffitall
     );
 
+foreach (values %Targets) {
+  $_ = abs_from_top($_);
+}
+
 {
   my @files = keys %Targets;
   my $filesopts = join(" | ", map { "--build-$_" } "all", sort @files);
@@ -45,7 +62,9 @@ $0: Usage: $0 [--verbose] [--showfiles] $filesopts
 __USAGE__
   unless @ARGV
        && GetOptions (verbose => \$Verbose,
+                      quiet => \$Quiet,
                       showfiles => \$showfiles,
+                      test => \$Test,
                       map {+"build-$_", \$Build{$_}} @files, 'all');
   # Set them all to true
   @Build{@files} = @files if ($Build{all});
@@ -78,8 +97,6 @@ if ($Verbose) {
   print "I'm building $_\n" foreach grep {$Build{$_}} keys %Build;
 }
 
-chdir $FindBin::Bin or die "$0: Can't chdir $FindBin::Bin: $!";
-
 open MASTER, $masterpodfile or die "$0: Can't open $masterpodfile: $!";
 
 my ($delta_source, $delta_target);
@@ -108,13 +125,14 @@ foreach (<MASTER>) {
     $flags{aux} = 1 if $flags =~ tr/a//d;
 
     if ($flags =~ tr/D//d) {
-      $flags{perlpod_omit} = 1;
+      $flags{manifest_omit} = 1;
       $delta_source = "$filename.pod";
     }
     if ($flags =~ tr/d//d) {
-      $flags{manifest_omit} = 1;
+      $flags{perlpod_omit} = 1;
       $delta_target = "$filename.pod";
     }
+    $Generated{"$filename.pod"}++ if $flags =~ tr/g//d;
 
     if ($flags =~ tr/r//d) {
       my $readme = $filename;
@@ -138,8 +156,7 @@ if (defined $delta_source) {
   if (defined $delta_target) {
     # This way round so that keys can act as a MANIFEST skip list
     # Targets will aways be in the pod directory. Currently we can only cope
-    # with sources being in the same directory. Fix this and do perlvms.pod
-    # with this?
+    # with sources being in the same directory.
     $Copies{$delta_target} = $delta_source;
   } else {
     die "$0: delta source defined but not target";
@@ -157,23 +174,25 @@ close MASTER;
   my (@manireadmes, %manireadmes);
   my (@perlpods, %perlpods);
   my (%our_pods);
-  my (%sources);
 
   # Convert these to a list of filenames.
   foreach (keys %Pods, keys %Readmepods) {
     $our_pods{"$_.pod"}++;
   }
 
-  # None of these filenames will be boolean false
-  @disk_pods = glob("*.pod");
-  @disk_pods{@disk_pods} = @disk_pods;
+  opendir my $dh, abs_from_top('pod/');
+  while (readdir $dh) {
+    next unless /\.pod\z/;
+    push @disk_pods, $_;
+    ++$disk_pods{$_};
+  }
 
   # Things we copy from won't be in perl.pod
   # Things we copy to won't be in MANIFEST
-  @sources{values %Copies} = ();
 
-  open(MANI, "../MANIFEST") || die "$0: opening ../MANIFEST failed: $!";
-  while (<MANI>) {
+  my $filename = abs_from_top('MANIFEST');
+  open my $mani, '<', $filename or die "$0: opening $filename failed: $!";
+  while (<$mani>) {
     if (m!^pod/([^.]+\.pod)\s+!i) {
       push @manipods, $1;
     } elsif (m!^README\.(\S+)\s+!i) {
@@ -181,54 +200,74 @@ close MASTER;
       push @manireadmes, "perl$1.pod";
     }
   }
-  close(MANI);
+  close $mani or die $!;
   @manipods{@manipods} = @manipods;
   @manireadmes{@manireadmes} = @manireadmes;
 
-  open(PERLPOD, "perl.pod") || die "$0: opening perl.pod failed: $!\n";
-  while (<PERLPOD>) {
+  $filename = abs_from_top('pod/perl.pod');
+  open my $perlpod, '<', $filename or die "$0: opening $filename failed: $!\n";
+  while (<$perlpod>) {
     if (/^For ease of access, /../^\(If you're intending /) {
       if (/^\s+(perl\S*)\s+\w/) {
        push @perlpods, "$1.pod";
       }
     }
   }
-  close(PERLPOD);
+  close $perlpod or die $!;
   die "$0: could not find the pod listing of perl.pod\n"
     unless @perlpods;
   @perlpods{@perlpods} = @perlpods;
 
+  my @inconsistent;
   foreach my $i (sort keys %disk_pods) {
-    warn "$0: $i exists but is unknown by buildtoc\n"
+    push @inconsistent, "$0: $i exists but is unknown by buildtoc\n"
       unless $our_pods{$i};
-    warn "$0: $i exists but is unknown by ../MANIFEST\n"
-      if !$manipods{$i} && !$manireadmes{$i} && !$Copies{$i};
-    warn "$0: $i exists but is unknown by perl.pod\n"
-       if !$perlpods{$i} && !exists $sources{$i};
+    push @inconsistent, "$0: $i exists but is unknown by ../MANIFEST\n"
+      if !$manipods{$i} && !$manireadmes{$i} && !$Copies{$i} && !$Generated{$i};
+    push @inconsistent, "$0: $i exists but is unknown by perl.pod\n"
+       if !$perlpods{$i} && !exists $Copies{$i};
   }
+  my @BuildTargets = grep {defined} @Targets{grep $_ ne 'all', keys %Build};
+  my %BuildFiles;
+  @BuildFiles{@BuildTargets} = @BuildTargets;
+
   foreach my $i (sort keys %our_pods) {
-    warn "$0: $i is known by buildtoc but does not exist\n"
-      unless $disk_pods{$i};
+    push @inconsistent, "$0: $i is known by buildtoc but does not exist\n"
+      unless $disk_pods{$i} or $BuildFiles{$i};
   }
   foreach my $i (sort keys %manipods) {
-    warn "$0: $i is known by ../MANIFEST but does not exist\n"
+    push @inconsistent, "$0: $i is known by ../MANIFEST but does not exist\n"
       unless $disk_pods{$i};
+    push @inconsistent, "$0: $i is known by ../MANIFEST but is marked as generated\n"
+      if $Generated{$i};
   }
   foreach my $i (sort keys %perlpods) {
-    warn "$0: $i is known by perl.pod but does not exist\n"
-      unless $disk_pods{$i};
+    push @inconsistent, "$0: $i is known by perl.pod but does not exist\n"
+      unless $disk_pods{$i} or $BuildFiles{$i};
+  }
+  if ($Test) {
+    print "1..1\n";
+    if (@inconsistent) {
+      print "not ok 1\n";
+      die @inconsistent
+    }
+    print "ok 1\n";
+    exit;
+  }
+  else {
+    warn @inconsistent if @inconsistent;
   }
 }
 
-# Find all the mdoules
+# Find all the modules
 {
   my @modpods;
-  find \&getpods => qw(../lib ../ext);
+  find \&getpods => abs_from_top('lib/');
 
   sub getpods {
     if (/\.p(od|m)$/) {
       my $file = $File::Find::name;
-      return if $file eq '../lib/Pod/Functions.pm'; # Used only by pod itself
+      return if $file =~ qr!/lib/Pod/Functions.pm\z!; # Used only by pod itself
       return if $file =~ m!(?:^|/)t/!;
       return if $file =~ m!lib/Attribute/Handlers/demo/!;
       return if $file =~ m!lib/Net/FTP/.+\.pm!; # Hi, Graham! :-)
@@ -237,7 +276,6 @@ close MASTER;
       return if $file =~ m!XS/(?:APItest|Typemap)!;
       my $pod = $file;
       return if $pod =~ s/pm$/pod/ && -e $pod;
-      die "$0: tut $File::Find::name" if $file =~ /TUT/;
       unless (open (F, "< $_\0")) {
        warn "$0: bogus <$file>: $!";
        system "ls", "-l", $file;
@@ -247,11 +285,12 @@ close MASTER;
        while ($line = <F>) {
          if ($line =~ /^=head1\s+NAME\b/) {
            push @modpods, $file;
-           #warn "GOOD $file\n";
+           close F;
            return;
          }
        }
-       warn "$0: $file: cannot find =head1 NAME\n";
+       close F;
+       warn "$0: $file: cannot find =head1 NAME\n" unless $Quiet;
       }
     }
   }
@@ -260,15 +299,15 @@ close MASTER;
 
   my %done;
   for (@modpods) {
-    #($name) = /(\w+)\.p(m|od)$/;
-    my $name = path2modname($_);
+    my $name = $_;
+    $name =~ s/\.p(m|od)$//;
+    $name =~ s-.*?/lib/--;
+    $name =~ s-/-::-g;
+    next if $done{$name}++;
+
     if ($name =~ /^[a-z]/) {
       $Pragmata{$name} = $_;
     } else {
-      if ($done{$name}++) {
-       # warn "already did $_\n";
-       next;
-      }
       $Modules{$name} = $_;
     }
   }
@@ -277,23 +316,12 @@ close MASTER;
 # OK. Now a lot of ancillary function definitions follow
 # Main program returns at "Do stuff"
 
-sub path2modname {
-    local $_ = shift;
-    s/\.p(m|od)$//;
-    s-.*?/(lib|ext)/--;
-    s-/-::-g;
-    s/(\w+)::\1/$1/;
-    return $_;
-}
-
-sub output ($);
+my $OUT;
 
 sub output_perltoc {
-  open(OUT, ">perltoc.pod") || die "$0: creating perltoc.pod failed: $!";
+  my $filename = shift;
 
-  local $/ = '';
-
-  ($_= <<"EOPOD2B") =~ s/^\t//gm && output($_);
+  ($_= <<"EOPOD2B") =~ s/^\t//gm and $OUT .= $_;
 
        # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
        # This file is autogenerated by buildtoc from all the other pods.
@@ -315,10 +343,12 @@ EOPOD2B
 #' make emacs happy
 
   # All the things in the master list that happen to be pod filenames
-  podset(map {"$_->[1].pod"} grep {defined $_ && @$_ == 3 && !$_->[0]{toc_omit}} @Master);
+  foreach (grep {defined $_ && @$_ == 3 && !$_->[0]{toc_omit}} @Master) {
+    podset($_->[1], abs_from_top("pod/$_->[1].pod"));
+  }
 
 
-  ($_= <<"EOPOD2B") =~ s/^\t//gm && output($_);
+  ($_= <<"EOPOD2B") =~ s/^\t//gm and $OUT .= $_;
 
 
 
@@ -326,9 +356,11 @@ EOPOD2B
 
 EOPOD2B
 
-  podset(sort values %Pragmata);
+  foreach (sort keys %Pragmata) {
+    podset($_, $Pragmata{$_});
+  }
 
-  ($_= <<"EOPOD2B") =~ s/^\t//gm && output($_);
+  ($_= <<"EOPOD2B") =~ s/^\t//gm and $OUT .= $_;
 
 
 
@@ -336,7 +368,9 @@ EOPOD2B
 
 EOPOD2B
 
-  podset( @Modules{ sort keys %Modules } );
+  foreach (sort keys %Modules) {
+    podset($_, $Modules{$_});
+  }
 
   $_= <<"EOPOD2B";
 
@@ -364,8 +398,16 @@ EOPOD2B
 EOPOD2B
 
   s/^\t//gm;
-  output $_;
-  output "\n";                    # flush $LINE
+  $OUT .= "$_\n";
+
+  $OUT =~ s/\n\s+\n/\n\n/gs;
+  $OUT =~ s/\n{3,}/\n\n/g;
+
+  $OUT =~ s/([^\n]+)/wrap('', '', $1)/ge;
+
+  open OUT, '>', $filename or die "$0: creating $filename failed: $!";
+  print OUT $OUT;
+  close OUT;
 }
 
 # Below are all the auxiliary routines for generating perltoc.pod
@@ -373,40 +415,39 @@ EOPOD2B
 my ($inhead1, $inhead2, $initem);
 
 sub podset {
-    local @ARGV = @_;
-    my $pod;
+    my ($pod, $file) = @_;
 
-    while(<>) {
+    local $/ = '';
+
+    open my $fh, '<', $file or die "Can't open file '$file' for $pod: $!";
+
+    while(<$fh>) {
        tr/\015//d;
        if (s/^=head1 (NAME)\s*/=head2 /) {
-           $pod = path2modname($ARGV);
            unhead1();
-           output "\n \n\n=head2 ";
-           $_ = <>;
+           $OUT .= "\n\n=head2 ";
+           $_ = <$fh>;
            # Remove svn keyword expansions from the Perl FAQ
            s/ \(\$Revision: \d+ \$\)//g;
-           if ( /^\s*$pod\b/ ) {
+           if ( /^\s*\Q$pod\E\b/ ) {
                s/$pod\.pm/$pod/;       # '.pm' in NAME !?
-               output $_;
            } else {
                s/^/$pod, /;
-               output $_;
            }
-           next;
        }
-       if (s/^=head1 (.*)/=item $1/) {
+       elsif (s/^=head1 (.*)/=item $1/) {
            unhead2();
-           output "=over 4\n\n" unless $inhead1;
+           $OUT .= "=over 4\n\n" unless $inhead1;
            $inhead1 = 1;
-           output $_; nl(); next;
+           $_ .= "\n";
        }
-       if (s/^=head2 (.*)/=item $1/) {
+       elsif (s/^=head2 (.*)/=item $1/) {
            unitem();
-           output "=over 4\n\n" unless $inhead2;
+           $OUT .= "=over 4\n\n" unless $inhead2;
            $inhead2 = 1;
-           output $_; nl(); next;
+           $_ .= "\n";
        }
-       if (s/^=item ([^=].*)/$1/) {
+       elsif (s/^=item ([^=].*)/$1/) {
            next if $pod eq 'perldiag';
            s/^\s*\*\s*$// && next;
            s/^\s*\*\s*//;
@@ -414,24 +455,23 @@ sub podset {
            s/\s+$//;
            next if /^[\d.]+$/;
            next if $pod eq 'perlmodlib' && /^ftp:/;
-           ##print "=over 4\n\n" unless $initem;
-           output ", " if $initem;
+           $OUT .= ", " if $initem;
            $initem = 1;
            s/\.$//;
            s/^-X\b/-I<X>/;
-           output $_; next;
        }
-       if (s/^=cut\s*\n//) {
-           unhead1();
+       else {
+           unhead1() if /^=cut\s*\n/;
            next;
        }
+       $OUT .= $_;
     }
 }
 
 sub unhead1 {
     unhead2();
     if ($inhead1) {
-       output "\n\n=back\n\n";
+       $OUT .= "\n\n=back\n\n";
     }
     $inhead1 = 0;
 }
@@ -439,45 +479,18 @@ sub unhead1 {
 sub unhead2 {
     unitem();
     if ($inhead2) {
-       output "\n\n=back\n\n";
+       $OUT .= "\n\n=back\n\n";
     }
     $inhead2 = 0;
 }
 
 sub unitem {
     if ($initem) {
-       output "\n\n";
-       ##print "\n\n=back\n\n";
+       $OUT .= "\n\n";
     }
     $initem = 0;
 }
 
-sub nl {
-    output "\n";
-}
-
-my $NEWLINE = 0;       # how many newlines have we seen recently
-my $LINE;              # what remains to be printed
-
-sub output ($) {
-    for (split /(\n)/, shift) {
-       if ($_ eq "\n") {
-           if ($LINE) {
-               print OUT wrap('', '', $LINE);
-               $LINE = '';
-           }
-           if (($NEWLINE) < 2) {
-               print OUT;
-               $NEWLINE++;
-           }
-       }
-       elsif (/\S/ && length) {
-           $LINE .= $_;
-           $NEWLINE = 0;
-       }
-    }
-}
-
 # End of original buildtoc. From here on are routines to generate new sections
 # for and inplace edit other files
 
@@ -514,7 +527,7 @@ sub generate_perlpod {
 
 
 sub generate_manifest {
-  # Annyoingly unexpand doesn't consider it good form to replace a single
+  # Annoyingly, unexpand doesn't consider it good form to replace a single
   # space before a tab with a tab
   # Annoyingly (2) it returns read only values.
   my @temp = unexpand (map {sprintf "%-32s%s\n", @$_} @_);
@@ -522,10 +535,12 @@ sub generate_manifest {
 }
 sub generate_manifest_pod {
   generate_manifest map {["pod/$_.pod", $Pods{$_}]}
-    grep {!$Copies{"$_.pod"}} sort keys %Pods;
+    sort grep {!$Copies{"$_.pod"}} grep {!$Generated{"$_.pod"}} keys %Pods;
 }
 sub generate_manifest_readme {
-  generate_manifest map {["README.$_", $Readmes{$_}]} sort keys %Readmes;
+  generate_manifest sort {$a->[0] cmp $b->[0]}
+    ["README.vms", "Notes about installing the VMS port"],
+      map {["README.$_", $Readmes{$_}]} keys %Readmes;
 }
 
 sub generate_roffitall {
@@ -549,14 +564,19 @@ sub generate_descrip_mms_1 {
 }
 
 sub generate_descrip_mms_2 {
-  map {sprintf <<'SNIP', $_, $_ eq 'perlvms' ? 'vms' : 'pod', $_}
-[.lib.pods]%s.pod : [.%s]%s.pod
-       @ If F$Search("[.lib]pods.dir").eqs."" Then Create/Directory [.lib.pods]
-       Copy/NoConfirm/Log $(MMS$SOURCE) [.lib.pods]
+  map {<<"SNIP"}
+[.lib.pods]$_.pod : [.pod]$_.pod
+       \@ If F\$Search("[.lib]pods.dir").eqs."" Then Create/Directory [.lib.pods]
+       Copy/NoConfirm/Log \$(MMS\$SOURCE) [.lib.pods]
 SNIP
    sort keys %Pods, keys %Readmepods;
 }
 
+sub generate_descrip_mms_3 {
+  map qq{\t- If F\$Search("[.pod]$_").nes."" Then Delete/NoConfirm/Log [.pod]$_;*},
+    sort keys %Generated, keys %Copies;
+}
+
 sub generate_nmake_1 {
   # XXX Fix this with File::Spec
   (map {sprintf "\tcopy ..\\README.%-8s ..\\pod\\perl$_.pod\n", $_}
@@ -569,9 +589,10 @@ sub generate_nmake_2 {
   # Spot the special case
   local $Text::Wrap::columns = 76;
   my $line = wrap ("\t    ", "\t    ",
-                  join " ", sort keys %Copies,
-                                 map {"perl$_.pod"} "vms", keys %Readmes);
+                  join " ", sort keys %Copies, keys %Generated,
+                                 map {"perl$_.pod"} keys %Readmes);
   $line =~ s/$/ \\/mg;
+  $line =~ s/ \\$//;
   $line;
 }
 
@@ -585,6 +606,13 @@ sub generate_pod_mak {
   $line;
 }
 
+sub verify_contiguous {
+  my ($name, $content, $what) = @_;
+  my $sections = () = $content =~ m/\0+/g;
+  croak("$0: $name contains no $what") if $sections < 1;
+  croak("$0: $name contains discontiguous $what") if $sections > 1;
+}
+
 sub do_manifest {
   my $name = shift;
   my @manifest =
@@ -604,15 +632,13 @@ sub do_nmake {
   my $makefile = join '', @_;
   die "$0: $name contains NUL bytes" if $makefile =~ /\0/;
   $makefile =~ s/^\tcopy \.\.\\README.*\n/\0/gm;
-  my $sections = () = $makefile =~ m/\0+/g;
-  die "$0: $name contains no README copies" if $sections < 1;
-  die "$0: $name contains discontiguous README copies" if $sections > 1;
+  verify_contiguous($name, $makefile, 'README copies');
   # Now remove the other copies that follow
   1 while $makefile =~ s/\0\tcopy .*\n/\0/gm;
   $makefile =~ s/\0+/join ("", &generate_nmake_1)/se;
 
-  $makefile =~ s{(del /f [^\n]+podchecker[^\n]+).*?(pod2html)}
-    {"$1\n" . &generate_nmake_2."\n\t    $2"}se;
+  $makefile =~ s{(-cd \$\(PODDIR\) && del /f[^\n]+).*?(-cd \.\.\\utils && del /f)}
+    {"$1\n" . &generate_nmake_2."\n\t$2"}se;
   $makefile;
 }
 
@@ -651,10 +677,7 @@ sub do_vms {
   my $makefile = join '', @_;
   die "$0: $name contains NUL bytes" if $makefile =~ /\0/;
   $makefile =~ s/\npod\d* =[^\n]*/\0/gs;
-  my $sections = () = $makefile =~ m/\0+/g;
-  die "$0: $name contains no pod assignments" if $sections < 1;
-  die "$0: $name contains $sections discontigous pod assignments"
-    if $sections > 1;
+  verify_contiguous($name, $makefile, 'pod assignments');
   $makefile =~ s/\0+/join "\n", '', &generate_descrip_mms_1/se;
 
   die "$0: $name contains NUL bytes" if $makefile =~ /\0/;
@@ -668,11 +691,15 @@ sub do_vms {
                 [^\n]+\n       # Another line
                 [^\n]+\Q[.lib.pods]\E\n                # ends [.lib.pods]
                    /\0/gsx;
-  $sections = () = $makefile =~ m/\0+/g;
-  die "$0: $name contains no copy rules" if $sections < 1;
-  die "$0: $name contains $sections discontigous copy rules"
-    if $sections > 1;
+  verify_contiguous($name, $makefile, 'copy rules');
   $makefile =~ s/\0+/join "\n", '', &generate_descrip_mms_2/se;
+
+# Looking for rules like this:
+#      - If F$Search("[.pod]perldelta.pod").nes."" Then Delete/NoConfirm/Log [.pod]perldelta.pod;*
+  $makefile =~ s!(?:\t- If F\$Search\("\[\.pod\]perl\d*[a-z]+\Q.pod").nes."" Then Delete/NoConfirm/Log [.pod]perl\E\d*[a-z]+\.pod;\*\n)+!\0!sg;
+  verify_contiguous($name, $makefile, 'delete rules');
+  $makefile =~ s/\0+/join "\n", &generate_descrip_mms_3, ''/se;
+
   $makefile;
 }
 
@@ -681,18 +708,27 @@ sub do_unix {
   my $makefile_SH = join '', @_;
   die "$0: $name contains NUL bytes" if $makefile_SH =~ /\0/;
 
-  $makefile_SH =~ s/\n\s+-\@test -f \S+ && cd pod && \$\(LNS\) \S+ \S+ && cd \.\. && echo "\S+" >> extra.pods \# See buildtoc\n/\0/gm;
+  $makefile_SH =~ s{^(perltoc_pod_prereqs = extra.pods).*}
+                  {join ' ', $1, map "pod/$_",
+                       sort keys %Copies, grep {!/perltoc/} keys %Generated
+                   }mge;
+
+# pod/perl511delta.pod: pod/perldelta.pod
+#      cd pod && $(LNS) perldelta.pod perl511delta.pod
 
-  my $sections = () = $makefile_SH =~ m/\0+/g;
+  $makefile_SH =~ s!(
+pod/perl[a-z0-9_]+\.pod: pod/perl[a-z0-9_]+\.pod
+       \$\(LNS\) perl[a-z0-9_]+\.pod pod/perl[a-z0-9_]+\.pod
+)+!\0!gm;
 
-  die "$0: $name contains no copy rules" if $sections < 1;
-  die "$0: $name contains $sections discontigous copy rules"
-    if $sections > 1;
+  verify_contiguous($name, $makefile_SH, 'copy rules');
 
-  my @copy_rules = map "\t-\@test -f pod/$Copies{$_} && cd pod && \$(LNS) $Copies{$_} $_ && cd .. && echo \"pod/$_\" >> extra.pods # See buildtoc",
-    keys %Copies;
+  my @copy_rules = map "
+pod/$_: pod/$Copies{$_}
+       \$(LNS) $Copies{$_} pod/$_
+", keys %Copies;
 
-  $makefile_SH =~ s/\0+/join "\n", '', @copy_rules, ''/se;
+  $makefile_SH =~ s/\0+/join '', @copy_rules/se;
   $makefile_SH;
 
 }
@@ -701,16 +737,18 @@ sub do_unix {
 
 my $built;
 while (my ($target, $name) = each %Targets) {
+  print "Working on target $target\n" if $Verbose;
   next unless $Build{$target};
   $built++;
   if ($target eq "toc") {
     print "Now processing $name\n" if $Verbose;
-    &output_perltoc;
+    output_perltoc($name);
     print "Finished\n" if $Verbose;
     next;
   }
   print "Now processing $name\n" if $Verbose;
   open THING, $name or die "Can't open $name: $!";
+  binmode THING;
   my @orig = <THING>;
   my $orig = join '', @orig;
   close THING;
@@ -723,10 +761,13 @@ while (my ($target, $name) = each %Targets) {
     print "Was not modified\n" if $Verbose;
     next;
   }
+  my $mode = (stat $name)[2] // die "$0: Can't stat $name: $!";
   rename $name, "$name.old" or die "$0: Can't rename $name to $name.old: $!";
   open THING, ">$name" or die "$0: Can't open $name for writing: $!";
+  binmode THING;
   print THING $new or die "$0: print to $name failed: $!";
-  close THING or die die "$0: close $name failed: $!";
+  close THING or die "$0: close $name failed: $!";
+  chmod $mode & 0777, $name or die "$0: can't chmod $mode $name: $!";
 }
 
 warn "$0: was not instructed to build anything\n" unless $built;