in reply to Re: Using hashref values in constant declarations.
in thread Using hashref values in constant declarations.

In terms of workarounds ...

For the benefit of the novice PerlMonk, it should be emphasized that none of the workarounds address (nor, I'm sure, were intended to address) the total mutability of the content of "constant" references:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use constant { ONE => 1, FOUR => 4 }; use constant AREF => [ ONE .. FOUR ]; dd AREF; ;; AREF->[3] = 99; dd AREF; ;; AREF->[9] = 999; dd AREF; ;; $#{ +AREF } = -1; dd AREF; " [1, 2, 3, 4] [1, 2, 3, 99] [1, 2, 3, 99, undef, undef, undef, undef, undef, 999] []
A search of MetaCPAN for "lock" yields a couple of interesting-looking modules, neither of which have I used and so cannot recommend: Data::Lock and Array::Lock.

In general, Perl handles the important concept of immutability very poorly. I have the vague notion that Perl 6 does better in this respect; can anyone comment?


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Using hashref values in constant declarations.
by LanX (Saint) on Sep 10, 2017 at 10:32 UTC
Re^3: Using hashref values in constant declarations.
by haukex (Archbishop) on Sep 11, 2017 at 06:08 UTC
    A search of MetaCPAN for "lock"

    This reminds me of the core module Hash::Util:

    #!/usr/bin/env perl use warnings; use strict; use Hash::Util qw/lock_hash/; my %x = ( foo => "bar" ); lock_hash %x; eval { $x{y}++; 1 } or warn $@; eval { $x{foo}="quz"; 1 } or warn $@; __END__ Attempt to access disallowed key 'y' in a restricted hash at lock.pl l +ine 9. Modification of a read-only value attempted at lock.pl line 10.

    Although there was some discussion on P5P about removing them a while back, I'm not sure if that will happen.

    For completeness, there are also tied hashes, which could implement the same thing.