| 1 |
#!/usr/local/bin/new/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
$/ = ''; |
| 5 |
$^I = '.bak'; |
| 6 |
|
| 7 |
my @undo; |
| 8 |
my $edits = 0; |
| 9 |
my $seen_NAME = 0; |
| 10 |
my @pend; |
| 11 |
|
| 12 |
while (<>) |
| 13 |
{ |
| 14 |
if (/^=for/ && !$seen_NAME) |
| 15 |
{ |
| 16 |
print "\n"; |
| 17 |
push(@pend,$_); |
| 18 |
} |
| 19 |
else |
| 20 |
{ |
| 21 |
print; |
| 22 |
if (@pend && $seen_NAME) |
| 23 |
{ |
| 24 |
print @pend; |
| 25 |
$edits += @pend; |
| 26 |
@pend = (); |
| 27 |
} |
| 28 |
if (/^=head1\s+NAME/) |
| 29 |
{ |
| 30 |
$seen_NAME = 1; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if (eof) |
| 35 |
{ |
| 36 |
warn "$edits $ARGV\n"; |
| 37 |
if (defined $^I) |
| 38 |
{ |
| 39 |
push(@undo,$ARGV) unless $edits; |
| 40 |
} |
| 41 |
$edits = 0; |
| 42 |
$seen_NAME = 0; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
foreach my $file (@undo) |
| 47 |
{ |
| 48 |
if (-f "$file$^I") |
| 49 |
{ |
| 50 |
unlink($file) if -f $file; |
| 51 |
rename("$file$^I",$file); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
# $edits += s/^(\s*\n=for.*)(\n=head1\s+NAME\s*\n)/$2\n$1/s; |
| 57 |
|