Skip to content

Implement Hash::Util::lock_keys restricted hashes #1172

Description

@fglock

Summary

Implement restricted hashes for Hash::Util::lock_keys (and the corresponding
query/unlock operations). PerlOnJava currently registers lock_keys but
returns without changing the hash, on both the JVM and interpreter backends.

Expected Perl behaviour

use Hash::Util qw(lock_keys);

my %hash = (foo => undef);
lock_keys(%hash);

eval { my $value = $hash{bar} };
print $@;

On standard Perl, the access dies with:

Attempt to access disallowed key 'bar' in a restricted hash

Internals::SvREADONLY(%hash) is true after lock_keys, and permitted keys
can be inspected with Hash::Util::legal_keys.

Actual PerlOnJava behaviour

The same program succeeds with no exception on both backends. The hash is not
marked readonly/restricted and its key set is not enforced.

The current Java implementation labels lock_keys and unlock_keys as
placeholder implementations. This also explains the existing feature-matrix
entry that marks restricted hashes unsupported.

CPAN evidence

Types::ReadOnly 0.003 failed in CPAN tester run 20260827-171032-2227.
Its Locked[...] constraints use Hash::Util::lock_keys plus
Internals::SvREADONLY to recognize restricted hash references. The affected
tests were t/03locked.t, t/10combinations.t, t/20moo.t, t/30moose.t,
and t/50typeparams.t: 13 of 60 subtests failed across 5 of 8 test programs.

Ecosystem impact

MetaCPAN reports 79 unique Hash::Util reverse-dependant distributions, 12
updated in the preceding 12 months. That total covers all Hash::Util APIs,
not only restricted hashes. Inspection of five representative dependants found
active lock_keys use in:

  • DBIx-DataModel runtime schema metadata, to prevent typo-created keys.
  • Net-Amazon-S3 shipped API/client parameter-validation helpers.
  • CPAN-Testers-ParseReport option-parsing utility.

The same sample found only non-restricted APIs in Sereal-Encoder
(num_buckets test) and Function-Parameters (fieldhash test).

Suggested acceptance coverage

  • Lock an existing hash and reject reads, writes, and deletes for disallowed
    keys using Perl-compatible diagnostics.
  • Support explicitly declared allowed-but-absent keys.
  • Make legal_keys report the declared key set.
  • Verify unlock_keys restores ordinary hash behaviour.
  • Ensure Internals::SvREADONLY reports the restricted-hash state expected by
    Type::Tiny/Types::ReadOnly.
  • Exercise equivalent behavior on the JVM and interpreter backends.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions