CPAN modules for defining constants
I've published a review of CPAN modules for defining constants. This covers the following modules:
Attribute::Constant • Config::Constants • Const::Fast • constant • constant::def • constant::defer • constant::lexical • constant::our • Constant::Generate • Constant::FromGlobal • Devel::Constants • ex::constant::vars • Readonly • Readonly::XS • Scalar::Constant • Scalar::Readonly
This blog engine had become too restrictive for writing these reviews, so from now on they'll be static web pages, with a reference here. Comments can be left here, or you can email me at neilb at cpan dot org.
Any particular reason enum wasn't included? :)
I only learned about enum a day or so ago, and that caused me to drop constant::Atom, which was going to be included. As I say in the introduction:
Had another look at enum. I'll be adding it in. It's traditional for the first comment to point out at least one module that I'd missed :-)
Since that page is UTF-8 encoded and uses Hebrew characters as well as diacritics, you might want to add the following line to the top your HTML header section so browsers will decode from UTF-8 by default.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Thanks -- done!
Personally, as far as I'm concerned a constant is only a true constant if the compiler can take advantage and curry out code based on it.
I'd suggest that anything for which this isn't true should just be considered a "readonly" variable.
As for why Readonly performs so badly, see #59690. The author's refusal to fix bugs in Readonly was my motivation for writing Const::Fast in the first place.
Oops, I meant #59689
Neil strikes again! Thanks for the article. I'm currently perusing your previous article.
This article would need a little bit of historical background.
First was Larry, who disliked C++ so much that he didn't want:
* that constants be called const,
* to add a type attribute, such as my const $i = 0;
Then somebody found out that constants do just fine uppercase as sub without sigil so that they look like PHP constants, which gave birth to the first constant package, i.e. sub {value}. Internally those "constants" were optimized to newCONSTSUB() with a CVf_CONST attribute. Scalars, AV and HV always carried an internal SVf_READONLY flag but this was abused for other things, constant hashes e.g. have been called restricted, PL_strtab was such a restricted hash.
Then Damien came up with the "tie" idea to check every write-access, which made the module Readonly messy and slow, but allowed arrays and hashes.
Finally Leon used with Const::Fast the internal SVf_READONLY flags consistently for SV, AV and HV.
Since constant folding was heavily crippled by Nick because integer overflows were not checked, the possible internal optimizations are merely moot. The most important part internally would be constant stashes and hashes, but there's still the "restricted" burden. So e.g. Moose has to do their own slow class->make_immutable thing.
Thanks Leon. I've updated to include a reference to the bug and also added Readonly::Scalar to the scalar benchmark, as using that will get you the Readonly::XS benefits.
Reini: s/Leon/Dan Kogai/
Have added enum, and enum::fields too.
BTW: https://github.com/rurban/perl/tree/typed/const was my start for a real native const/readonly attribute for lexical variables, but without the following compiler optimizations for perfect hashes, better arrays, methods, classes, inheritance, ...