dgaramond2 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

What is the cause the error message: 'Modification of non-creatable hash value attempted, subscript "_" at ...'? perldiag is not very helpful. I'm loading hashes from a YAML document and am trying to see if some entries are invalid (say $u->{foo}), and then undef-ing them ($u->{foo} = undef) if they are. On some values of $u, this always happen and the invalid entries happen to be undef ($u->{foo} = undef). This stops happening if I set $u->{foo} to an empty string ('').

  • Comment on Modification of non-creatable hash value attempted

Replies are listed 'Best First'.
Re: Modification of non-creatable hash value attempted
by moritz (Cardinal) on Mar 07, 2008 at 09:40 UTC
    There are CPAN modules that make hashes non-modifiable, like Readonly.

    Maybe $u is such a read-only hash? Or a tied hash that doesn't support modification?

    It would help if you could provide a small sample that lets us reproduce the problem.

Re: Modification of non-creatable hash value attempted
by akho (Hermit) on Mar 07, 2008 at 10:25 UTC
    On an unrelated note: use delete to remove items from a hash. If you just set them to undef, the hash keys stay (and they will be present in any loop through the hash):

    use warnings; use strict; my %a; $a{a} = undef; $a{b} = 1; print keys %a, "\n"; # prints 'ab' delete $a{b}; print keys %a, "\n"; # prints 'a'
Re: Modification of non-creatable hash value attempted
by pascaldee (Acolyte) on Mar 08, 2008 at 00:34 UTC

    Thanks for the responses. I can't use delete() because I need the keypair to still be there. Haven't tried to create a simplified script+data to reproduce this, but the problem is fully reproducible on my real script+data. Worked around it by avoiding undef values in the first place, so my problem is solved for now. Still a bit curious about the error message and why it happened though.

      Modification of non-creatable hash value attempted, %s (P) You tried to make a hash value spring into existence, and it couldn't be created for some peculiar reason.

      (P) An internal error you should never see (trappable).

Re: Modification of non-creatable hash value attempted
by Anonymous Monk on Jul 16, 2008 at 17:58 UTC

    I found it in my XS code too.

    if a XS function stores static variables like &PL_sv_undef, &PL_sv_yes, &PL_sv_no into hashes and you want overwrite the hash entry, you will get this error.

    for example:

    void
    xs_fnc()
    PREINIT:
      HV *hv;
    PPCODE:
      hv = newHV();
      hv_store( hv, "key", 3, &PL_sv_undef, 0 );
      ST(0) = sv_2mortal( newRV( sv_2mortal( (SV *) hv ) ) );
      XSRETURN(1);
    
    $hashref = xs_fnc();
    $hashref->{'key'} = 'bla'; # error