summaryrefslogtreecommitdiff
path: root/Porting/corelist-perldelta.pl
blob: c036a18607f959fa46819ea6cc3dff078a1e0aea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
#!perl
use 5.010;
use strict;
use warnings;
use lib 'Porting';
use Maintainers qw/%Modules/;
use lib 'dist/Module-CoreList/lib';
use Module::CoreList;
use Getopt::Long;

=head1 USAGE

  # generate the module changes for the Perl you are currently building
  ./perl -Ilib Porting/corelist-perldelta.pl

  # update the module changes for the Perl you are currently building
  ./perl -Ilib Porting/corelist-perldelta.pl --mode=update pod/perldelta.pod

  # generate a diff between the corelist sections of two perldelta* files:
  perl Porting/corelist-perldelta.pl --mode=check 5.017001 5.017002 <perl5172delta.pod

=head1 ABOUT

corelist-perldelta.pl is a bit schizophrenic. The part to generate the
new Perldelta text does not need Algorithm::Diff, but wants to be
run with the freshly built Perl.

The part to check the diff wants to be run with a Perl that has an up-to-date
L<Module::CoreList>, but needs the outside L<Algorithm::Diff>.

Ideally, the program will be split into two separate programs, one
to generate the text and one to show the diff between the
corelist sections of the last perldelta and the next perldelta.

Currently no information about Removed Modules is displayed in any of the
modes.

=cut

my %sections = (
  new     => qr/New Modules and Pragma(ta)?/,
  updated => qr/Updated Modules and Pragma(ta)?/,
  removed => qr/Removed Modules and Pragma(ta)?/,
);

my %titles = (
  new     => 'New Modules and Pragmata',
  updated => 'Updated Modules and Pragmata',
  removed => 'Removed Modules and Pragmata',
);

my $deprecated;

sub run {
  my %opt = (mode => 'generate');

  GetOptions(\%opt,
    'mode|m:s', # 'generate', 'check', 'update'
  );

  # by default, compare latest two version in CoreList;
  my ($old, $new) = latest_two_perl_versions();

  # use the provided versions if present
  # @ARGV >=2 means [old_version] [new_version] [path/to/file]
  if ( @ARGV >= 2) {
    ($old, $new) = (shift @ARGV, shift @ARGV);
    die "$old is an invalid version\n" if not exists
      $Module::CoreList::version{$old};
    die "$new is an invalid version\n" if not exists
      $Module::CoreList::version{$new};
  }

  if ( $opt{mode} eq 'generate' ) {
    do_generate($old => $new);
  }
  elsif ( $opt{mode} eq 'check' ) {
    do_check(\*ARGV, $old => $new);
  }
  elsif ( $opt{mode} eq 'update' ) {
    do_update_existing(shift @ARGV, $old => $new);
  }
  else {
    die "Unrecognized mode '$opt{mode}'\n";
  }

  exit 0;
}

sub latest_two_perl_versions {

  my @versions = sort keys %Module::CoreList::version;

  my $new = pop @versions;

  # If a fully-padded version number ends in a zero (as in "5.019010"), that
  # version shows up in %Module::CoreList::version both with and without its
  # trailing zeros. So skip all versions that are numerically equal to $new.
  pop @versions while @versions && $versions[-1] == $new;

  die "Too few distinct core versions in %Module::CoreList::version ?!\n"
    if !@versions;

  return $versions[-1], $new;
}

# Given two perl versions, it returns a list describing the core distributions that have changed.
# The first three elements are hashrefs corresponding to new, updated, and removed modules
# and are of the form (mostly, see the special remarks about removed):
#   'Distribution Name' => ['Distribution Name', previous version number, current version number]
# where the version number is undef if the distribution did not exist.
# The fourth element is an arrayref of core distribution names of those distribution for which it
# is unknown whether they have changed and therefore need to be manually checked.
#
# In most cases, the distribution name in %Modules corresponds to the module that is representative
# of the distribution as listed in Module::CoreList. However, there are a few distribution names
# that do not correspond to a module. %distToModules has been created which maps the distribution
# name to a representative module. The representative module was chosen by either looking at the
# Makefile of the distribution or by seeing which module the distribution has been traditionally
# listed under in past perldeltas.
#
# There are a few distributions for which there is no single representative module (e.g. libnet).
# These distributions are returned as the last element of the list.
#
# %Modules contains a final key, _PERLLIB, which contains a list of modules that are owned by p5p.
# This list contains modules and pragmata that may also be present in Module::CoreList.
# A list of modules are in the list @unclaimedModules, which were manually listed based on whether
# they were independent modules and whether they have been listed in past perldeltas.
# The pragmata were found by doing something like:
#   say for sort grep { $_ eq lc $_ and !exists $Modules{$_}}
#     keys %{$Module::CoreList::version{'5.019003'}}
# and manually filtering out pragamata that were already covered.
#
# It is currently not possible to differentiate between a removed module and a removed
# distribution. Therefore, the removed hashref contains every module that has been removed, even if
# the module's corresponding distribution has not been removed.

sub corelist_delta {
  my ($old, $new) = @_;
  my $corelist = \%Module::CoreList::version;
  my %changes = Module::CoreList::changes_between( $old, $new );
  $deprecated = $Module::CoreList::deprecated{$new};

  my $getModifyType = sub {
    my $data = shift;
    if ( exists $data->{left} and exists $data->{right} ) {
      return 'updated';
    }
    elsif ( !exists $data->{left} and exists $data->{right} ) {
      return 'new';
    }
    elsif ( exists $data->{left} and !exists $data->{right} ) {
      return 'removed';
    }
    return undef;
  };

  my @unclaimedModules = qw/AnyDBM_File B B::Concise B::Deparse Benchmark Class::Struct Config::Extensions DB
                            DBM_Filter Devel::Peek DirHandle DynaLoader English Errno ExtUtils::Embed ExtUtils::Miniperl
                            ExtUtils::Typemaps ExtUtils::XSSymSet Fcntl File::Basename File::Compare File::Copy File::DosGlob
                            File::Find File::Glob File::stat FileCache FileHandle FindBin GDBM_File Getopt::Std Hash::Util Hash::Util::FieldHash
                            I18N::Langinfo IPC::Open3 NDBM_File ODBM_File Opcode PerlIO PerlIO::encoding PerlIO::mmap PerlIO::scalar PerlIO::via
                            Pod::Functions Pod::Html POSIX SDBM_File SelectSaver Symbol Sys::Hostname Thread Tie::Array Tie::Handle Tie::Hash
                            Tie::Hash::NamedCapture Tie::Memoize Tie::Scalar Tie::StdHandle Tie::SubstrHash Time::gmtime Time::localtime Time::tm
                            Unicode::UCD UNIVERSAL User::grent User::pwent VMS::DCLsym VMS::Filespec VMS::Stdio XS::Typemap XS::APItest Win32CORE/;
  my @unclaimedPragmata = qw/arybase attributes blib bytes charnames deprecate diagnostics encoding feature fields filetest inc::latest integer less locale mro open ops overload overloading re sigtrap sort strict subs utf8 vars vmsish/;
  my @unclaimed = (@unclaimedModules, @unclaimedPragmata);

  my %distToModules = (
    'IO-Compress' => [
      {
        'name'         => 'IO-Compress',
        'modification' => $getModifyType->( $changes{'IO::Compress::Base'} ),
        'data'         => $changes{'IO::Compress::Base'}
      }
    ],
    'libnet' => [
      {
        'name'         => 'libnet',
        'modification' => $getModifyType->( $changes{'Net::Cmd'} ),
        'data'         => $changes{'Net::Cmd'}
      }
    ],
    'PathTools' => [
      {
        'name'         => 'File::Spec',
        'modification' => $getModifyType->( $changes{'Cwd'} ),
        'data'         => $changes{'Cwd'}
      }
    ],
    'podlators' => [
      {
        'name'         => 'podlators',
        'modification' => $getModifyType->( $changes{'Pod::Text'} ),
        'data'         => $changes{'Pod::Text'}
      }
    ],
    'Scalar-List-Utils' => [
      {
        'name'         => 'List::Util',
        'modification' => $getModifyType->( $changes{'List::Util'} ),
        'data'         => $changes{'List::Util'}
      },
      {
        'name'         => 'Scalar::Util',
        'modification' => $getModifyType->( $changes{'Scalar::Util'} ),
        'data'         => $changes{'Scalar::Util'}
      },
      {
        'name'         => 'Sub::Util',
        'modification' => $getModifyType->( $changes{'Sub::Util'} ),
        'data'         => $changes{'Sub::Util'}
      }
    ],
    'Text-Tabs+Wrap' => [
      {
        'name'         => 'Text::Tabs',
        'modification' => $getModifyType->( $changes{'Text::Tabs'} ),
        'data'         => $changes{'Text::Tabs'}
      },
      {
        'name'         => 'Text::Wrap',
        'modification' => $getModifyType->( $changes{'Text::Wrap'} ),
        'data'         => $changes{'Text::Wrap'}
      }
    ],
  );

  # structure is (new|removed|updated) => [ [ModuleName, previousVersion, newVersion] ]
  my $deltaGrouping = {};

  # list of distributions listed in %Modules that need to be manually checked because there is no module that represents it
  my @manuallyCheck;

  # %Modules defines what is currently in core
  for my $k ( keys %Modules ) {
    next if $k eq '_PERLLIB'; #these are taken care of by being listed in @unclaimed
    next if Module::CoreList::is_core($k) and !exists $changes{$k}; #modules that have not changed

    my ( $distName, $modifyType, $data );

    if ( exists $changes{$k} ) {
      $distName   = $k;
      $modifyType = $getModifyType->( $changes{$k} );
      $data       = $changes{$k};
    }
    elsif ( exists $distToModules{$k} ) {
      # modification will be undef if the distribution has not changed
      my @modules = grep { $_->{modification} } @{ $distToModules{$k} };
      for (@modules) {
        $deltaGrouping->{ $_->{modification} }->{ $_->{name} } = [ $_->{name}, $_->{data}->{left}, $_->{data}->{right} ];
      }
      next;
    }
    else {
      push @manuallyCheck, $k and next;
    }

    $deltaGrouping->{$modifyType}->{$distName} = [ $distName, $data->{left}, $data->{right} ];
  }

  for my $k (@unclaimed) {
    if ( exists $changes{$k} ) {
      $deltaGrouping->{ $getModifyType->( $changes{$k} ) }->{$k} =
        [ $k, $changes{$k}->{left}, $changes{$k}->{right} ];
    }
  }

  # in old corelist, but not this one => removed
  # N.B. This is exhaustive -- not just what's in %Modules, so modules removed from
  # distributions will show up here, too.  Some person will have to review to see what's
  # important. That's the best we can do without a historical Maintainers.pl
  for my $k ( keys %{ $corelist->{$old} } ) {
    if ( ! exists $corelist->{$new}{$k} ) {
      $deltaGrouping->{'removed'}->{$k} = [ $k, $corelist->{$old}{$k}, undef ];
    }
  }

  return (
    \%{ $deltaGrouping->{'new'} },
    \%{ $deltaGrouping->{'removed'} },
    \%{ $deltaGrouping->{'updated'} },
    \@manuallyCheck
  );
}

# currently does not update the Removed Module section
sub do_update_existing {
  my ( $existing, $old, $new ) = @_;

  my ( $added, $removed, $updated, $manuallyCheck ) = corelist_delta( $old => $new );
  if (@{$manuallyCheck}) {
    print "It cannot be determined whether the following distributions have changed.\n";
    print "Please check and list accordingly:\n";
    say "\t* $_" for sort @{$manuallyCheck};
    print "\n";
  }

  my $data = {
    new      => $added,
    updated  => $updated,
    #removed => $removed, ignore removed for now
  };

  my $text = DeltaUpdater::transform_pod( $existing, $data );
  open my $out, '>', $existing or die "can't open perldelta file $existing: $!";
  binmode($out);
  print $out $text;
  close $out;
  say "The New and Updated Modules and Pragamata sections in $existing have been updated";
  say "Please ensure the Removed Modules and Pragmata section is up-to-date";
}

sub do_generate {
  my ($old, $new) = @_;
  my ($added, $removed, $updated, $manuallyCheck) = corelist_delta($old => $new);

  if ($manuallyCheck) {
    print "\nXXXIt cannot be determined whether the following distributions have changed.\n";
    print "Please check and list accordingly:\n";
    say "\t$_" for @{$manuallyCheck};
    print "\n";
  }

  my $data = {
    new      => $added,
    updated  => $updated,
    #removed => $removed, ignore removed for now
  };

  say DeltaUpdater::sections_to_pod($data)
}

sub do_check {
  my ($in, $old, $new) = @_;

  my $delta = DeltaParser->new($in);
  my ($added, $removed, $updated) = corelist_delta($old => $new);

  # because of the difficulty in identifying the distribution for removed modules
  # don't bother checking them
  for my $ck ([ 'new', $delta->new_modules, $added ],
              #[ 'removed', $delta->removed_modules, $removed ],
              [ 'updated', $delta->updated_modules, $updated ] ) {
    my @delta = @{ $ck->[1] };
    my @corelist = sort { lc $a->[0] cmp lc $b->[0] } values %{ $ck->[2] };

    printf $ck->[0] . ":\n";

    require Algorithm::Diff;
    my $diff = Algorithm::Diff->new(map {
      [map { join q{ } => grep defined, @{ $_ } } @{ $_ }]
    } \@delta, \@corelist);

    while ($diff->Next) {
      next if $diff->Same;
      my $sep = '';
      if (!$diff->Items(2)) {
        printf "%d,%dd%d\n", $diff->Get(qw( Min1 Max1 Max2 ));
      } elsif(!$diff->Items(1)) {
        printf "%da%d,%d\n", $diff->Get(qw( Max1 Min2 Max2 ));
      } else {
        $sep = "---\n";
        printf "%d,%dc%d,%d\n", $diff->Get(qw( Min1 Max1 Min2 Max2 ));
      }
      print "Delta< $_\n" for $diff->Items(1);
      print $sep;
      print "Corelist> $_\n" for $diff->Items(2);
    }

    print "\n";
  }
}

{

  package DeltaUpdater;
  use List::Util 'reduce';

  sub get_section_name_from_heading {
    my $heading = shift;
    while (my ($key, $expression) = each %sections) {
      if ($heading =~ $expression) {
        return $titles{$key};
      }
    }
    die "$heading did not match any section";
  }

  sub is_desired_section_name {
    for (values %sections) {
      return 1 if $_[0] =~ $_;
    }
    return 0;
  }

  # verify the module and pragmata in the section, changing the stated version if necessary
  # this subroutine warns if the module name cannot be parsed or if it is not listed in
  # the results returned from corelist_delta()
  #
  # a side-effect of calling this function is that modules present in the section are
  # removed from $data, resulting in $data containing only those modules and pragmata
  # that were not listed in the perldelta file. This means we can then pass $data to
  # add_to_section() without worrying about filtering out duplicates
  sub update_section {
    my ( $section, $data, $title ) = @_;
    my @items = @{ $section->{items} };

    for my $item (@items) {

      my $content = $item->{text};
      my $module  = $item->{name};

      #skip dummy items
      next if !$module and $content =~ /\s*xx*\s*/i;

      say "Could not parse module name; line is:\n\t$content" and next unless $module;

      if ( !$data->{$title}{$module} ) {
        print "$module is not listed as being $title in Module::CoreList.\n";
        print "Ensure Module::CoreList has been updated and\n";
        print "check to see that the distribution is not listed under another name.\n\n";
        next;
      }

      if ( $title eq 'new' ) {
        my ($new) = $content =~ /(\d[^\s]+)\s+has\s+been.*$/m;
        say "Could not parse new version for $module; line is:\n\t$content" and next unless $new;
        if ( $data->{$title}{$module}[2] ne $new ) {
            say "$module: new version differs; version in pod: $new; version in corelist: " . $data->{$title}{$module}[2];
        }
        $content =~ s/\d[^\s]+(\s+has\s+been.*$)/$data->{$title}{$module}[2].$1/me;
      }

      elsif ( $title eq 'updated' ) {
        my ( $prev, $new ) = $content =~ /from\s+(?:version\s+)?(\d[^\s]+)\s+to\s+(?:version\s+)?(\d[^\s,]+?)(?=[\s,]|\.\s|\.$|$).*/s;
        say "Could not parse old and new version for $module; line is:\n\t$content" and next
          unless $prev and $new;
        if ( $data->{$title}{$module}[1] ne $prev ) {
          say "$module: previous version differs; version in pod: $prev; version in corelist: " . $data->{$title}{$module}[1];
        }
        if ( $data->{$title}{$module}[2] ne $new ) {
          say "$module: new version differs; version in pod: $new; version in corelist: " . $data->{$title}{$module}[2];
        }
        $content =~
          s/(from\s+(?:version\s+)?)\d[^\s]+(\s+to\s+(?:version\s+)?)\d[^\s,]+?(?=[\s,]|\.\s|\.$|$)(.*)/$1.$data->{$title}{$module}[1].$2.$data->{$title}{$module}[2].$3/se;
      }

      elsif ( $title eq 'removed' ) {
        my ($prev) = $content =~ /^.*?was\s+(\d[^\s]+?)/m;
        say "Could not parse old version for $module; line is:\n\t$content" and next unless $prev;
        if ( $data->{$title}{$module}[1] ne $prev ) {
          say "$module: previous version differs; $prev " . $data->{$title}{$module}[1];
        }
        $content =~ s/(^.*?was\s+)\d[^\s]+?/$1.$data->{$title}{$module}[1]/me;
      }

      delete $data->{$title}{$module};
      $item->{text} = $content;
    }
    return $section;
  }

  # add modules and pragmata present in $data to the section
  sub add_to_section {
    my ( $section, $data, $title ) = @_;

    #undef is a valid version name in Module::CoreList so suppress warnings about concatenating undef values
    no warnings 'uninitialized';
    for ( values %{ $data->{$title} } ) {
      my ( $mod, $old_v, $new_v ) = @{$_};
      my ( $item, $text );

      $item = { name => $mod, text => "=item *\n" };
      if ( $title eq 'new' ) {
        $text = "L<$mod> $new_v has been added to the Perl core.\n";
      }

      elsif ( $title eq 'updated' ) {
        $text = "L<$mod> has been upgraded from version $old_v to $new_v.\n";
        if ( $deprecated->{$mod} ) {
          $text .= "NOTE: L<$mod> is deprecated and may be removed from a future version of Perl.\n";
        }
      }

      elsif ( $title eq 'removed' ) {
        $text = "C<$mod> has been removed from the Perl core.  Prior version was $old_v.\n";
      }

      $item->{text} .= "\n$text\n";
      push @{ $section->{items} }, $item;
    }
    return $section;
  }

  sub sort_items_in_section {
    my ($section) = @_;

    # if we could not parse the module name, it will be uninitalized
    # in sort. This is not a problem as it will just result in these
    # sections being placed near the beginning of the section
    no warnings 'uninitialized';
    $section->{items} =
      [ sort { lc $a->{name} cmp lc $b->{name} } @{ $section->{items} } ];
    return $section;
  }

  # given a hashref of the form returned by corelist_delta()
  # and a hash structured as documented in transform_pod(), it returns
  # a pod string representation of the sections, creating sections
  # if necessary
  sub sections_to_pod {
    my ( $data, %sections ) = @_;
    my $out = '';

    for (
        (
          [ 'New Modules and Pragmata',     'new' ],
          [ 'Updated Modules and Pragmata', 'updated' ],
          [ 'Removed Modules and Pragmata', 'removed' ]
        )
      )
    {
      my ( $section_name, $title ) = @{$_};

      my $section = $sections{$section_name} // {
          name           => $section_name,
          preceding_text => "=head2 $_->[0]\n=over 4\n",
          following_text => "=back\n",
          items          => [],
          manual         => 1
      };

      $section = update_section( $section, $data, $title );
      $section = add_to_section( $section, $data, $title );
      $section = sort_items_in_section( $section );

      next if $section->{manual} and scalar @{ $section->{items} } == 0;

      my $items = reduce { no warnings 'once'; $a . $b->{text} }
        ( '', @{ $section->{items} } );
      $out .=
        ( $section->{preceding_text} // '' )
        . $items
        . ( $section->{following_text} // '' );
    }
    return $out;
  }

  # given a filename corresponding to an existing perldelta file
  # and a hashref of the form returned by corelist_delta(), it
  # returns a string of the resulting file after the module
  # information has been added.
  sub transform_pod {
    my ( $existing, $data ) = @_;

    # will contain hashrefs corresponding to new, updated and removed
    # modules and pragmata keyed by section name
    # each section is hashref of the structure
    #   preceding_text => Text occurring before and including the over
    #                     region containing the list of modules,
    #   items          => [Arrayref of hashrefs corresponding to a module
    #                      entry],
    #     an entry has the form:
    #       name => Module name or undef if the name could not be determined
    #       text => The text of the entry, including the item heading
    #
    #   following_text => Any text not corresponding to a module
    #                     that occurs after the first module
    #
    # the sections are converted to a pod string by calling sections_to_pod()
    my %sections;

    # we are in the Modules_and_Pragmata's section
    my $in_Modules_and_Pragmata;

    # we are the Modules_and_Pragmata's section but have not
    # encountered any of the desired sections. We use this
    # flag to determine whether we should append the text to $out
    # or we need to delay appending until the module listings are
    # processed and instead append to $append_to_out
    my $in_Modules_and_Pragmata_preamble;

    my $done_processing_Modules_and_Pragmata;

    my $current_section;

    # $nested_element_level == 0 : not in an over region, treat lines as text
    # $nested_element_level == 1 : presumably in the top over region that
    #                              corresponds to the module listing. Treat
    #                              each item as a module
    # $nested_element_level > 1  : we only consider these values when we are in an item
    #                              We treat lines as the text of the current item.
    my $nested_element_level = 0;

    my $current_item;
    my $need_to_parse_module_name;

    my $out = '';
    my $append_to_out = '';

    open my $fh, '<', $existing or die "can't open perldelta file $existing: $!";
    binmode($fh);

    while (<$fh>) {
      # treat the rest of the file as plain text
      if ($done_processing_Modules_and_Pragmata) {
        $out .= $_;
        next;
      }

      elsif ( !$in_Modules_and_Pragmata ) {
        # entering Modules and Pragmata
        if (/^=head1 Modules and Pragmata/) {
          $in_Modules_and_Pragmata          = 1;
          $in_Modules_and_Pragmata_preamble = 1;
        }
        $out .= $_;
        next;
      }

      # leaving Modules and Pragmata
      elsif (/^=head1/) {
        if ($current_section) {
          push @{ $current_section->{items} }, $current_item
            if $current_item;
          $sections{ $current_section->{name} } = $current_section;
        }
        $done_processing_Modules_and_Pragmata = 1;
        $out .=
          sections_to_pod( $data, %sections ) . $append_to_out . $_;
        next;
      }

      # new section in Modules and Pragmata
      elsif (/^=head2 (.*?)$/) {
        my $name = $1;
        if ($current_section) {
          push @{ $current_section->{items} }, $current_item
            if $current_item;
          $sections{ $current_section->{name} } = $current_section;
          undef $current_section;
        }

        if ( is_desired_section_name($name) ) {
          undef $in_Modules_and_Pragmata_preamble;
          if ( $nested_element_level > 0 ) {
            die "Unexpected head2 at line no. $.";
          }
          my $title = get_section_name_from_heading($name);
          if ( exists $sections{$title} ) {
            die "$name occurred twice at line no. $.";
          }
          $current_section                   = {};
          $current_section->{name}           = $title;
          $current_section->{preceding_text} = $_;
          $current_section->{items}          = [];
         $nested_element_level               = 0;
          next;
        }

        # otherwise treat section as plain text
        else {
          if ($in_Modules_and_Pragmata_preamble) {
            $out .= $_;
          }
          else {
            $append_to_out .= $_;
          }
          next;
        }
      }

      elsif ($current_section) {

        # not in an over region
        if ( $nested_element_level == 0 ) {
          if (/^=over/) {
            $nested_element_level++;
          }
          if ( scalar @{ $current_section->{items} } > 0 ) {
            $current_section->{following_text} .= $_;
          }
          else {
            $current_section->{preceding_text} .= $_;
          }
          next;
        }

        if ($current_item) {
          if ($need_to_parse_module_name) {
            # the item may not have a parsable module name, which means that
            # $current_item->{name} will never be defined.
            if (/^(?:L|C)<(.+?)>/) {
              $current_item->{name} = $1;
              undef $need_to_parse_module_name;
            }
            # =item or =back signals the end of an item
            # block, which we handle below
            if ( !/^=(?:item|back)/ ) {
              $current_item->{text} .= $_;
              next;
            }
          }
          # currently in an over region
          # treat text inside region as plain text
          if ( $nested_element_level > 1 ) {
            if (/^=back/) {
              $nested_element_level--;
            }
            elsif (/^=over/) {
              $nested_element_level++;
            }
            $current_item->{text} .= $_;
            next;
          }
          # entering over region
          if (/^=over/) {
            $nested_element_level++;
            $current_item->{text} .= $_;
            next;
          }
          # =item or =back signals the end of an item
          # block, which we handle below
          if ( !/^=(?:item|back)/ ) {
            $current_item->{text} .= $_;
            next;
          }
        }

        if (/^=item \*/) {
          push @{ $current_section->{items} }, $current_item
            if $current_item;
          $current_item = { text => $_ };
          $need_to_parse_module_name = 1;
          next;
        }

        if (/^=back/) {
          push @{ $current_section->{items} }, $current_item
            if $current_item;
          undef $current_item;
          $nested_element_level--;
        }

        if ( scalar @{ $current_section->{items} } == 0 ) {
          $current_section->{preceding_text} .= $_;
        }
        else {
          $current_section->{following_text} .= $_;
        }
        next;
      }

      # text in Modules and Pragmata not in a head2 region
      else {
        if ($in_Modules_and_Pragmata_preamble) {
          $out .= $_;
        }
        else {
          $append_to_out .= $_;
        }
        next;
      }
    }
    close $fh;
    die 'Never saw Modules and Pragmata section' unless $in_Modules_and_Pragmata;
    return $out;
  }

}

{
  package DeltaParser;
  use Pod::Simple::SimpleTree;

  sub new {
    my ($class, $input) = @_;

    my $self = bless {} => $class;

    my $parsed_pod = Pod::Simple::SimpleTree->new->parse_file($input)->root;
    splice @{ $parsed_pod }, 0, 2; # we don't care about the document structure,
                                   # just the nodes within it

    $self->_parse_delta($parsed_pod);

    return $self;
  }

  # creates the accessor methods:
  #   new_modules
  #   updated_modules
  #   removed_modules
  for my $k (keys %sections) {
    no strict 'refs';
    my $m = "${k}_modules";
    *$m = sub { $_[0]->{$m} };
  }

  sub _parse_delta {
    my ($self, $pod) = @_;

    my $new_section     = $self->_look_for_section( $pod, $sections{new} );
    my $updated_section = $self->_look_for_section( $pod, $sections{updated} );
    my $removed_section = $self->_look_for_section( $pod, $sections{removed} );

    $self->_parse_new_section($new_section);
    $self->_parse_updated_section($updated_section);
    $self->_parse_removed_section($removed_section);

    for (qw/new_modules updated_modules removed_modules/) {
      $self->{$_} =
        [ sort { lc $a->[0] cmp lc $b->[0] } @{ $self->{$_} } ];
    }

    return;
  }

  sub _parse_new_section {
    my ($self, $section) = @_;

    $self->{new_modules} = [];
    return unless $section;
    $self->{new_modules} = $self->_parse_section($section => sub {
      my ($el) = @_;

      my ($first, $second) = @{ $el }[2, 3];
      my ($ver) = $second =~ /(\d[^\s]+)\s+has\s+been/;

      return [ $first->[2], undef, $ver ];
    });

    return;
  }

  sub _parse_updated_section {
    my ($self, $section) = @_;

    $self->{updated_modules} = [];
    return unless $section;
    $self->{updated_modules} = $self->_parse_section($section => sub {
      my ($el) = @_;

      my ($first, $second) = @{ $el }[2, 3];
      my $module = $first->[2];

      # the regular expression matches the following:
      #   from VERSION_NUMBER to VERSION_NUMBER
      #   from VERSION_NUMBER to VERSION_NUMBER.
      #   from version VERSION_NUMBER to version VERSION_NUMBER.
      #   from VERSION_NUMBER to VERSION_NUMBER and MODULE from VERSION_NUMBER to VERSION_NUMBER
      #   from VERSION_NUMBER to VERSION_NUMBER, and MODULE from VERSION_NUMBER to VERSION_NUMBER
      #
      # some perldeltas contain more than one module listed in an entry, this only attempts to match the
      # first module
      my ($old, $new) = $second =~
          /from\s+(?:version\s+)?(\d[^\s]+)\s+to\s+(?:version\s+)?(\d[^\s,]+?)(?=[\s,]|\.\s|\.$|$).*/s;

      warn "Unable to extract old or new version of $module from perldelta"
        if !defined $old || !defined $new;

      return [ $module, $old, $new ];
    });

    return;
  }

  sub _parse_removed_section {
    my ($self, $section) = @_;

    $self->{removed_modules} = [];
    return unless $section;
    $self->{removed_modules} = $self->_parse_section($section => sub {
      my ($el) = @_;

      my ($first, $second) = @{ $el }[2, 3];
      my ($old) = $second =~ /was\s+(\d[^\s]+?)\.?$/;

      return [ $first->[2], $old, undef ];
    });

    return;
  }

  sub _parse_section {
    my ($self, $section, $parser) = @_;

    my $items = $self->_look_down($section => sub {
      my ($el) = @_;
      return unless ref $el && $el->[0] =~ /^item-/
          && @{ $el } > 2 && ref $el->[2];
      return unless $el->[2]->[0] =~ /C|L/;

      return 1;
    });

    return [map { $parser->($_) } @{ $items }];
  }

  sub _look_down {
    my ($self, $pod, $predicate) = @_;
    my @pod = @{ $pod };

    my @l;
    while (my $el = shift @pod) {
      push @l, $el if $predicate->($el);
      if (ref $el) {
        my @el = @{ $el };
        splice @el, 0, 2;
        unshift @pod, @el if @el;
      }
    }

    return @l ? \@l : undef;
  }

  sub _look_for_section {
    my ($self, $pod, $section) = @_;

    my $level;
    $self->_look_for_range($pod,
      sub {
        my ($el) = @_;
        my ($heading) = $el->[0] =~ /^head(\d)$/;
        my $f = $heading && $el->[2] =~ /^$section/;
        $level = $heading if $f && !$level;
        return $f;
      },
      sub {
        my ($el) = @_;
        $el->[0] =~ /^head(\d)$/ && $1 <= $level;
      },
    );
  }

  sub _look_for_range {
    my ($self, $pod, $start_predicate, $stop_predicate) = @_;

    my @l;
    for my $el (@{ $pod }) {
      if (@l) {
        return \@l if $stop_predicate->($el);
      }
      else {
        next unless $start_predicate->($el);
      }
      push @l, $el;
    }

    return;
  }
}

run;