develooper Front page | perl.perl5.porters | Postings from March 2023

Re: Pre-RFC Deep constants

Thread Previous | Thread Next
March 11, 2023 15:50
Re: Pre-RFC Deep constants
Message ID:
On Sat, 11 Mar 2023 at 15:20, Matthew Persico <>

> How about an MVP that creates just scalar constants?

Huh? We have two ways to create "constant" scalars:

use constant var => "foo";
Internals::SvREADONLY((my $var = "x"),1);

Am I missing something?

> That's got to be 90% of what people need, and then we can add
> constant hashes later?

We have constant (aka read-only) hashes as well. It is just that they are
*restricted*, which renders them nearly useless for most purposes.

use Hash::Util qw(lock_hash_recurse);

The "restricted" means that unlike normal hashes they throw an exception
when you try to look something in them that they don't contain. (You can
use lock_keys() to specify keys that are allowed to be in the hash but
which do not yet have values.)  The restricted API is sufficiently useless
that I have never seen them used productively. I have /heard/ people claim
they like them on this list, but I have never seen them used to advantage
in my entire career, on the contrary every time I have seen someone try to
use them they have reversed it out almost immediately afterwards.
Ironically I wrote most of the modern code in Hash::Util to facilitate the
advanced forms of read-only hash. But I did not create restricted hashes

> After all, constant hashes are just a bunch of constants for those of us
> with namespace OCD (including myself!).

Like I said, it turns out that reasonable people can disagree on what a
constant hash should be. :-) I really wish we didnt have restricted hashes
getting in the way, simple "readonly" hashes would be much more useful than
restricted hashes are.

MMMM, I wonder if there is a way we can extend restricted hashes so they
behave as people would expect a readonly/constant hash to behave without
throwing out restricted hashes entirely. Maybe something like
lock_hash(\%hash, undef); ? Eg, the undef would signal "allow any key".
Hrmmmm...... Watch this space, maybe I can come up with a hack for this.


perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About