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.

14 Comments

Any particular reason enum wasn't included? :)

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">

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.

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.

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, ...

Leave a comment

About Neil Bowers

user-pic Perl hacker since 1992.