NAME

Readonly - Facility for creating read-only scalars, arrays, hashes

Synopsis

use Readonly;

# Read-only scalar
Readonly::Scalar    $sca => $initial_value;
Readonly::Scalar my $sca => $initial_value;

# Read-only array
Readonly::Array    @arr => @values;
Readonly::Array my @arr => @values;

# Read-only hash
Readonly::Hash    %has => (key => value, key => value, ...);
Readonly::Hash my %has => (key => value, key => value, ...);
# or:
Readonly::Hash    %has => {key => value, key => value, ...};

# You can use the read-only variables like any regular variables:
print $sca;
$something = $sca + $arr[2];
next if $has{$some_key};

# But if you try to modify a value, your program will die:
$sca = 7;
push @arr, 'seven';
delete $has{key};
# The error message is "Modification of a read-only value attempted"

# Alternate form (Perl 5.8 and later)
Readonly    $sca => $initial_value;
Readonly my $sca => $initial_value;
Readonly    @arr => @values;
Readonly my @arr => @values;
Readonly    %has => (key => value, key => value, ...);
Readonly my %has => (key => value, key => value, ...);

# Alternate form (for Perls earlier than v5.8)
Readonly    \$sca => $initial_value;
Readonly \my $sca => $initial_value;
Readonly    \@arr => @values;
Readonly \my @arr => @values;
Readonly    \%has => (key => value, key => value, ...);
Readonly \my %has => (key => value, key => value, ...);

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.

Comparison with typeglob constants

Another popular way to create read-only scalars is to modify the symbol table entry for the variable by using a typeglob:

*a = \'value';

This works fine, but it only works for global variables ("my" variables have no symbol table entry). Also, the following similar constructs do not work:

*a = [1, 2, 3];      # Does NOT create a read-only array
*a = { a => 'A'};    # Does NOT create a read-only hash

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:

# For testing:
Readonly::Scalar  $Foo_Directory => '/usr/local/foo';
Readonly::Scalar  $Bar_Directory => '/usr/local/bar';
# $Foo_Directory = '/usr/local/foo';
# $Bar_Directory = '/usr/local/bar';

# For production:
# Readonly::Scalar  $Foo_Directory => '/usr/local/foo';
# Readonly::Scalar  $Bar_Directory => '/usr/local/bar';
$Foo_Directory = '/usr/local/foo';
$Bar_Directory = '/usr/local/bar';

Functions

Examples

These are a few very simple examples:

Scalars

A plain old read-only value

Readonly::Scalar $a => "A string value";

The value need not be a compile-time constant:

Readonly::Scalar $a => $computed_value;

Arrays/Lists

A read-only array:

Readonly::Array @a => (1, 2, 3, 4);

The parentheses are optional:

Readonly::Array @a => 1, 2, 3, 4;

You can use Perl's built-in array quoting syntax:

Readonly::Array @a => qw/1 2 3 4/;

You can initialize a read-only array from a variable one:

Readonly::Array @a => @computed_values;

A read-only array can be empty, too:

Readonly::Array @a => ();
Readonly::Array @a;        # equivalent

Hashes

Typical usage:

Readonly::Hash %a => (key1 => 'value1', key2 => 'value2');

A read-only hash can be initialized from a variable one:

Readonly::Hash %a => %computed_values;

A read-only hash can be empty:

Readonly::Hash %a => ();
Readonly::Hash %a;        # equivalent

If you pass an odd number of values, the program will die:

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

Thanks to Ernest Lergon for the idea (and initial code) for deeply-Readonly data structures (21 May 2002).

Thanks to Damian Conway for the idea (and code) for making the Readonly function work a lot smoother under perl 5.8+.

Author

Sanko Robinson sanko@cpan.org - http://sankorobinson.com/

CPAN ID: SANKO

Original author: Eric J. Roode, roode@cpan.org

License and Legal

Copyright (C) 2013 by Sanko Robinson sanko@cpan.org

Copyright (c) 2001-2004 by Eric J. Roode. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.