Description

This is a facility for creating non-modifiable variables. This is useful for configuration files, headers, etc. It can also be useful as a development and debugging tool, for catching updates to variables that should not be changed.

If any of the values you pass to Scalar, Array, or Hash are references, then those functions recurse over the data structures, marking everything as Readonly. Usually, this is what you want: the entire structure nonmodifiable. If you want only the top level to be Readonly, use the alternate Scalar1, Array1 and Hash1 functions.

Please note that most users of Readonly will also want to install a companion module Readonly::XS. See the "Cons" section below for more details.

Comparison with "use constant"

Perl provides a facility for creating constant values, via the constant pragma. There are several problems with this pragma.

Pros

Readonly.pm, on the other hand, will work with global variables and with lexical ("my") variables. It will create scalars, arrays, or hashes, all of which look and work like normal, read-write Perl variables. You can use them in scalar context, in list context; you can take references to them, pass them to functions, anything.

Readonly.pm also works well with complex data structures, allowing you to tag the whole structure as nonmodifiable, or just the top level.

Also, Readonly variables may not be reassigned. The following code will die:

Readonly::Scalar $pi => 3.14159;
...
Readonly::Scalar $pi => 2.71828;

Cons

Readonly.pm does impose a performance penalty. It's pretty slow. How slow? Run the eg/benchmark.pl script that comes with Readonly. On my test system, "use constant", typeglob constants, and regular read/write Perl variables were all about the same speed, and Readonly.pm constants were about 1/20 the speed.

However, there is relief. There is a companion module available, Readonly::XS. If it is installed on your system, Readonly.pm uses it to make read-only scalars much faster. With Readonly::XS, Readonly scalars are as fast as the other types of variables. Readonly arrays and hashes will still be relatively slow. But it's likely that most of your Readonly variables will be scalars.

If you can't use Readonly::XS (for example, if you don't have a C compiler, or your perl is statically linked and you don't want to re-link it), you have to decide whether the benefits of Readonly variables outweigh the speed issue. For most configuration variables (and other things that Readonly is likely to be useful for), the speed issue is probably not really a big problem. But benchmark your program if it might be. If it turns out to be a problem, you may still want to use Readonly.pm during development, to catch changes to variables that should not be changed, and then remove it for production:

Functions

Creates a nonmodifiable scalar, $var, and assigns a value of $value to it. Thereafter, its value may not be changed. Any attempt to modify the value will cause your program to die.

A value must be supplied. If you want the variable to have undef as its value, you must specify undef.

If $value is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the $value marked as Readonly, use Scalar1.

If $var is already a Readonly variable, the program will die with an error about reassigning Readonly variables.

Readonly::Array @arr => (value, value, ...);

Creates a nonmodifiable array, @arr, and assigns the specified list of values to it. Thereafter, none of its values may be changed; the array may not be lengthened or shortened or spliced. Any attempt to do so will cause your program to die.

If any of the values passed is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the hash %@arr itself marked as Readonly, use Array1.

If @arr is already a Readonly variable, the program will die with an error about reassigning Readonly variables.

Readonly::Hash %h => (key => value, key => value, ...);

Readonly::Hash %h => {key => value, key => value, ...};

Creates a nonmodifiable hash, %h, and assigns the specified keys and values to it. Thereafter, its keys or values may not be changed. Any attempt to do so will cause your program to die.

A list of keys and values may be specified (with parentheses in the synopsis above), or a hash reference may be specified (curly braces in the synopsis above). If a list is specified, it must have an even number of elements, or the function will die.

If any of the values is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the hash %h itself marked as Readonly, use Hash1.

If %h is already a Readonly variable, the program will die with an error about reassigning Readonly variables.

Readonly $var => $value;

Readonly @arr => (value, value, ...);

Readonly %h => (key => value, ...);

Readonly %h => {key => value, ...};

The Readonly function is an alternate to the Scalar, Array, and Hash functions. It has the advantage (if you consider it an advantage) of being one function. That may make your program look neater, if you're initializing a whole bunch of constants at once. You may or may not prefer this uniform style.

It has the disadvantage of having a slightly different syntax for versions of Perl prior to 5.8. For earlier versions, you must supply a backslash, because it requires a reference as the first parameter.

Readonly::Hash %a => (key1 => 'value1', "value2");
# This dies with "May not store an odd number of values in a hash"

Exports

Historically, this module exports the Readonly symbol into the calling program's namespace by default. The following symbols are also available for import into your program, if you like: Scalar, Scalar1, Array, Array1, Hash, and Hash1.

Requirements

Readonly::XS is recommended but not required. There are no non-core requirements.

Bug Reports

If email is better for you, my address is mentioned below but I would rather have bugs sent through the issue tracker found at http://github.com/sanko/readonly/issues.

Please check the TODO file included with this distribution in case your bug is already known (...I probably won't file bug reports to myself).

Acknowladgements

Thanks to Slaven Rezic for the idea of one common function (Readonly) for all three types of variables (13 April 2002).