Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Weird hash order behaviour on several RedHat releases.

by pboin (Deacon)
on Mar 16, 2006 at 15:59 UTC ( [id://537181]=note: print w/replies, xml ) Need Help??


in reply to Weird hash order behaviour on several RedHat releases.

The only thing you're guaranteed is consistency between keys() and values(). Basically, don't ever count on keys being in order and you'll be OK.

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 perlsec).
  • Comment on Re: Weird hash order behaviour on several RedHat releases.

Replies are listed 'Best First'.
Re^2: Weird hash order behaviour on several RedHat releases.
by wazoox (Prior) on Mar 16, 2006 at 16:10 UTC
    I know that but
    Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see "Algorithmic Complexity Attacks" in perlsec)."
    And I have the same order for every run, on 3 different machines running different RedHat flavours and perl releases. I've also played with PERL_HASH_SEED, no luck, it has absolutely no effect at all. Why do RedHat do that ?

      I just wonder if Red Hat didn't turn the randomization off on purpose in the name of compatibility to make sure that older applications didn't break when keys started to come back randomly. I have no doc that says that's the case, that just seems reasonable to me. Here's the option to do it:

      From perlrun:

      The default behaviour is to randomise unless the PERL_HASH_SEED is set. If Perl has been compiled with -DUSE_HASH_SEED_EXPLICIT, the default behaviour is not to randomise unless the PERL_HASH_SEED is set.
        Actually I've checked output for perl -V :
        $ perl -V | grep SEED PERL_HASH_SEED="-1"
        But setting the PERL_HASH_SEED env variable has no effect at all. Using this script :
        #!/usr/bin/perl use strict; use warnings; use Hash::Util qw(hash_seed); my %t=qw( a A b B c C d D e E f F ); print hash_seed() . "\n"; print join( '', %t) ."\n";
        I change the PERL_HASH_SEED, the hash_seed differs from run to run, but the hash order remains the same:
        $ for i in -1 0 1 42 garbage; do export PERL_HASH_SEED=$i; for j in 1 +2 3; do perl test.pl; done; done 3657515954 eEcCaAbBdDfF 3070542819 eEcCaAbBdDfF 185686570 eEcCaAbBdDfF 0 eEcCaAbBdDfF 0 eEcCaAbBdDfF 0 eEcCaAbBdDfF 1 eEcCaAbBdDfF 1 eEcCaAbBdDfF 1 eEcCaAbBdDfF 42 eEcCaAbBdDfF 42 eEcCaAbBdDfF 42 eEcCaAbBdDfF 3381895560 eEcCaAbBdDfF 2632524130 eEcCaAbBdDfF 1729561246 eEcCaAbBdDfF
        The hash seed has no effect, that sucks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://537181]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found