From efa96da9de3a7cdc104fab92b20e3bf07a9faf98 Mon Sep 17 00:00:00 2001
From: Danny Milosavljevic <dannym@friendly-machines.com>
Date: Mon, 5 May 2025 21:46:16 +0200
Subject: [PATCH] Make doc generator reproducible.

Fixes <https://github.com/PDLPorters/pdl/issues/541>.
---
 lib/PDL/Doc.pm | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/PDL/Doc.pm b/lib/PDL/Doc.pm
index 106cd8704..50f132dad 100644
--- a/lib/PDL/Doc.pm
+++ b/lib/PDL/Doc.pm
@@ -495,15 +495,17 @@ sub savedb {
   my $hash = $this->ensuredb;
   open my $fh, '>', $this->{Outfile} or barf "can't write to symdb $this->{Outfile}: $!";
   binmode $fh;
-  while (my ($name,$mods_hash) = each %$hash) {
+  for my $name (sort keys %$hash) {
+    my $mods_hash = $hash->{$name};
     next if 0 == scalar(%$mods_hash);
-    while (my ($module,$val) = each %$mods_hash) {
+    for my $module (sort keys %$mods_hash) {
+      my $val = $mods_hash->{$module};
       my $fi = $val->{File};
       $val->{File} = abs2rel($fi, dirname($this->{Outfile}))
         #store paths to *.pm files relative to pdldoc.db
         if file_name_is_absolute($fi) && -f $fi;
       delete $val->{Dbfile}; # no need to store Dbfile
-      my $txt = join(chr(0),$name,$module,%$val);
+      my $txt = join(chr(0),$name,$module,map +($_=>$val->{$_}), sort keys %$val);
       print $fh pack("S",length($txt)).$txt;
     }
   }
@@ -679,7 +681,8 @@ sub scan {
   my $hash = $this->{SYMS} ||= {};
   my $n = 0;
   $_->{File} = $file2, $n++ for values %{ $parser->{SYMHASH} };
-  while (my ($key,$val) = each %{ $parser->{SYMHASH} }) {
+  for my $key (sort keys %{ $parser->{SYMHASH} }) {
+    my $val = $hash->{$key};
     #set up the 3-layer hash/database structure: $hash->{funcname}->{PDL::SomeModule} = $val
     if (defined($val->{Module})) {
 	$hash->{$key}{$val->{Module}} = $val;
@@ -741,7 +744,10 @@ sub scantree {
     $ntot += my $n = $this->scan($File::Find::name,$verbose);
     print "\t$n functions\n";
   };
-  File::Find::find($sub,$dir);
+  File::Find::find({
+    wanted => $sub,
+    preprocess => sub { sort @_ }
+  }, $dir);
   print "\nfound $ntot functions\n";
   $ntot;
 }
@@ -881,7 +887,7 @@ own code.
  print $pdldoc->gethash->{zeroes}->{PDL::Core}->{Ref};
 
  # Get info for all the functions whose examples use zeroes
- my @entries = $pdldoc->search('zeroes','Example',1);
+ my @entries = $pdldoc->search('zeroes','Example',1,1);
 
  # All the functions that use zeroes in their example:
  print "Functions that use 'zeroes' in their examples include:\n";
