Is Perl guaranteed to return consistently-ordered hash keys?

后端 未结 3 964
生来不讨喜
生来不讨喜 2021-01-12 01:57

Given something like

foreach (keys %myHash) {
   ... do stuff ...
}

foreach (keys %myHash) {
   ... do more stuff ...
}

Is Perl guaranteed

3条回答
  •  既然无缘
    2021-01-12 02:41

    Yes. From perldoc -f keys:

    The keys are returned in an apparently random order. The actual random order is subject to change in future versions of perl, but it is guaranteed to be the same order as either the values or each function produces (given that the hash has not been modified). Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see "Algorithmic Complexity Attacks" in perldoc perlsec).

    (emphasis mine)

提交回复
热议问题