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

Phoenix talks about using the constant pragma to create immutable scalars in the following:

http://www.perldoc.com/perl5.8.0/lib/constant.html

and gets into how constant references can address anonymous arrays, but this can be circumvented by assigning the reference to a new value(s). The same argument, I assume, can be made with hashes.

but this begs the question. Is there a way to lock the value of a hash such that each (key,value) pair contained cannot be altered? Oh, ye wise holders of the holiest of Perl knowledge, is there a way that this is supported by the language and/or module?

Thanks.

Replies are listed 'Best First'.
Re: constant hash values?
by gjb (Vicar) on Sep 26, 2003 at 23:51 UTC

    You may want to have a look at Hash::Util.

    Just my 2 cents, gjb

    Update: Sorry, I should have mentioned that Hash::Util is for Perl 5.8.x. It's part of the standard distribution, also in ActiveState Perl.
      Thanks for your response. OK, my bad. I see Hash::Util on CPAN, but I don't see it on ActiveState. Other than compiling it myself from CPAN, I would think that it must be on ActiveState somewhere? Do you know the incantation? Thanks again.
        Anonymous Monk,
        What version of AS do you have? Chances are if you have 5.8 (which is required for Hash::Util), you already have it. Try executing:
        c:\>perl -MHash::Util -e "print 'ok';"
        If that doesn't cause an error (be sure to specify path to perl.exe if not in PATH), then you are all set. If you do not have AS 5.8, you can upgrade. If you have 5.8 and it failed - you should probably re-install since you have at least one issue. Finally, if you need this functionality for an earlier version or you just want to learn for the sake of learning, you can create your own module by creating a tied hash. See perldoc perltie

        Cheers - L~R

Re: constant hash values?
by Starky (Chaplain) on Sep 27, 2003 at 21:11 UTC
    The mention of Hash::Util in earlier replies is an excellent suggestion.

    It is also worth mentioning that there is a way you can influence scalars, arrays, or hashes to exhibit arbitrary behavior. You can tie any variable to an object whose class can do just about whatever you want it to.

    Do a perdoc perltie and search for the strings Tying Hashes and TIEHASH for more information.

    Hope this helps!