snippet
liz
The subroutine <code>is_hash_randomized()</code> allows you to check whether the hash keys in your Perl have a random or repeatable order (as returned by <code>each</code> or <code>keys</code>. By default, in Perl 5.8.1, the order of hash keys is no longer repeatable: it will be different for each run of Perl.
<P>
The subroutine returns either a defined or an undefined value. If an undefined value is returned, then the order of hash keys is <B>not</B> repeatable. If a defined value if returned, it indicates the hash seed that was used to generate, and thereby order, the hash keys.
<P>
When called with a Perl < 5.8.1, the value "0" is always returned. Please note though, that the same value only guarantees repeatable hash key order only for a given version of Perl, <B>not</B> for different Perl versions.
<P>
<B>Update</B>:<BR>
Ok, that's all pretty forgettable. Perl 5.8.1 will have the the <code>hash_seed()</code> subroutine in the <code>Hash::Util</code> module. From the new pod:
<DL>
<DT><B>hash_seed</B>
<DD><code>my $hash_seed = hash_seed();</code>
<P>
hash_seed() returns the seed number used to randomise
hash ordering. Zero means the "traditional" random
hash ordering, non-zero means the new even more random
hash ordering introduced in Perl 5.8.1.
</DL>
<P>
It should be noted that this routine has slightly different semantics, as you can not discover whether you would or would not got different hash key orders for different runs of Perl. But it doesn't need to do any strange things, it's going directly at the Perl internals.
<CODE>
sub is_hash_randomized {
# We don't have any random sequences yet, as if seed "0"
return 0 if $] < 5.008001;
# Get the -Accflags= settings using this Perl executor
open my $handle, "$^X -V:ccflags |"
or die "Could not execute $^X -V:ccflags: $!\n";
my $ccflags = <$handle>;
close $handle;
# Return repeatable default if random sequences compiled out
# Return random if switch settings reversed and environment variable set
# Return whatever the environment variable indicates
return 0 if $ccflags =~ m#-DNO_HASH_SEED#;
return if $ccflags =~ m#-DUSE_HASH_SEED_EXPLICIT#
and exists $ENV{PERL_HASH_SEED};
$ENV{PERL_HASH_SEED};
} #is_hash_randomized
</CODE>