.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "ReadonlyX 3pm"
.TH ReadonlyX 3pm "2022-11-19" "perl v5.36.0" "User Contributed Perl Documentation"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
ReadonlyX \- Faster facility for creating read\-only scalars, arrays, hashes
.SH "Synopsis"
.IX Header "Synopsis"
.Vb 3
\&    use strict;
\&    use warnings;
\&    use ReadonlyX;
\&
\&    # Read\-only scalar
\&    my $sca1;
\&    Readonly::Scalar $sca1    => 3.14;
\&    Readonly::Scalar my $sca2 => time;
\&    Readonly::Scalar my $sca3 => \*(AqWelcome\*(Aq;
\&    my $sca4 = time();
\&    Readonly::Scalar $sca4; # Value is not clobbered
\&
\&    # Read\-only array
\&    my @arr1;
\&    Readonly::Array @arr1 => [1 .. 4];
\&
\&    # or:
\&    Readonly::Array my @arr2 => (1, 3, 5, 7, 9);
\&
\&    # Read\-only hash
\&    my %hash1;
\&    Readonly::Hash %hash1    => (key => \*(Aqvalue\*(Aq, key2 => \*(Aqvalue\*(Aq);
\&    Readonly::Hash my %hash2 => (key => \*(Aqvalue\*(Aq, key2 => \*(Aqvalue\*(Aq);
\&
\&    # or:
\&    Readonly::Hash my %hash3 => {key => \*(Aqvalue\*(Aq, key2 => \*(Aqvalue\*(Aq};
\&
\&    # You can use the read\-only variables like any regular variables:
\&    print $sca1;
\&    my $something = $sca1 + $arr1[2];
\&    warn \*(AqBlah!\*(Aq if $hash1{key2};
\&
\&    # But if you try to modify a value, your program will die:
\&    $sca2 = 7;           # "Modification of a read\-only value attempted"
\&    push @arr1, \*(Aqseven\*(Aq; # "Modification of a read\-only value attempted"
\&    $arr1[1] = \*(Aqnine\*(Aq;   # "Modification of a read\-only value attempted"
\&    delete $hash1{key};  # Attempt to delete readonly key \*(Aqkey\*(Aq from a restricted hash
\&
\&    # Create mutable clones
\&    Readonly::Scalar $scalar => {qw[this that]};
\&    # $scalar\->{\*(Aqeh\*(Aq} = \*(Aqfoo\*(Aq; # Modification of a read\-only value attempted
\&    my $scalar_clone = Readonly::Clone $scalar;
\&    $scalar_clone\->{\*(Aqeh\*(Aq} = \*(Aqfoo\*(Aq;
\&    # $scalar_clone is now {this => \*(Aqthat\*(Aq, eh => \*(Aqfoo\*(Aq};
.Ve
.SH "Description"
.IX Header "Description"
This is a near-drop-in replacement for Readonly, the popular 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.
.PP
If you really need to have immutable variables in new code, use this instead
of Readonly. You'll thank me later. See the section entitled
\&\*(L"ReadonlyX vs. Readonly\*(R" for more.
.SH "Functions"
.IX Header "Functions"
All of these functions can be imported into your package by name.
.SS "Readonly::Scalar"
.IX Subsection "Readonly::Scalar"
.Vb 3
\&    Readonly::Scalar $pi      => 3.14;
\&    Readonly::Scalar my $aref => [qw[this that]]; # list ref
\&    Readonly::Scalar my $href => {qw[this that]}; # hash ref
.Ve
.PP
Creates a non-modifiable scalar and assigns a value of to it. Thereafter, its
value may not be changed. Any attempt to modify the value will cause your
program to die.
.PP
If the given 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.
.PP
If the variable is already readonly, the program will die with an error about
reassigning readonly variables.
.SS "Readonly::Array"
.IX Subsection "Readonly::Array"
.Vb 2
\&    Readonly::Array @arr1    => [1 .. 4];
\&    Readonly::Array my @arr2 => (1, 3, 5, 7, 9);
.Ve
.PP
Creates a non-modifiable array 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. Any attempt to do so will cause your program to die.
.PP
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.
.PP
If the variable is already readonly, the program will die with an error about
reassigning readonly variables.
.SS "Readonly::Hash"
.IX Subsection "Readonly::Hash"
.Vb 2
\&    Readonly::Hash %h => (key => \*(Aqvalue\*(Aq, key2 => \*(Aqvalue\*(Aq);
\&    Readonly::Hash %h => {key => \*(Aqvalue\*(Aq, key2 => \*(Aqvalue\*(Aq};
.Ve
.PP
Creates a non-modifiable hash 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.
.PP
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.
.PP
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.
.PP
If the variable is already readonly, the program will die with an error about
reassigning readonly variables.
.SS "Readonly::Clone"
.IX Subsection "Readonly::Clone"
.Vb 1
\&    my $scalar_clone = Readonly::Clone $scalar;
.Ve
.PP
When cloning using Storable or Clone you will notice that the value
stays readonly, which is correct. If you want to clone the value without
copying the readonly flag, use this.
.PP
.Vb 5
\&    Readonly::Scalar my $scalar => {qw[this that]};
\&    # $scalar\->{\*(Aqeh\*(Aq} = \*(Aqfoo\*(Aq; # Modification of a read\-only value attempted
\&    my $scalar_clone = Readonly::Clone $scalar;
\&    $scalar_clone\->{\*(Aqeh\*(Aq} = \*(Aqfoo\*(Aq;
\&    # $scalar_clone is now {this => \*(Aqthat\*(Aq, eh => \*(Aqfoo\*(Aq};
.Ve
.PP
In this example, the new variable (\f(CW$scalar_clone\fR) is a mutable clone of the
original \f(CW$scalar\fR. You can change it like any other variable.
.SH "Examples"
.IX Header "Examples"
Here are a few very simple examples again to get you started:
.SS "Scalars"
.IX Subsection "Scalars"
A plain old read-only value:
.PP
.Vb 1
\&    Readonly::Scalar $a => "A string value";
.Ve
.PP
The value need not be a compile-time constant:
.PP
.Vb 1
\&    Readonly::Scalar $a => $computed_value;
.Ve
.PP
Need an undef constant? Okay:
.PP
.Vb 1
\&    Readonly::Scalar $a;
.Ve
.SS "Arrays/Lists"
.IX Subsection "Arrays/Lists"
A read-only array:
.PP
.Vb 1
\&    Readonly::Array @a => (1, 2, 3, 4);
.Ve
.PP
The parentheses are optional:
.PP
.Vb 1
\&    Readonly::Array @a => 1, 2, 3, 4;
.Ve
.PP
You can use Perl's built-in array quoting syntax:
.PP
.Vb 1
\&    Readonly::Array @a => qw[1 2 3 4];
.Ve
.PP
You can initialize a read-only array from a variable one:
.PP
.Vb 1
\&    Readonly::Array @a => @computed_values;
.Ve
.PP
A read-only array can be empty, too:
.PP
.Vb 3
\&    Readonly::Array @a => ();
\&    # or
\&    Readonly::Array @a;
.Ve
.SS "Hashes"
.IX Subsection "Hashes"
Typical usage:
.PP
.Vb 3
\&    Readonly::Hash %a => (key1 => \*(Aqvalue1\*(Aq, key2 => \*(Aqvalue2\*(Aq);
\&    # or
\&    Readonly::Hash %a => {key1 => \*(Aqvalue1\*(Aq, key2 => \*(Aqvalue2\*(Aq};
.Ve
.PP
A read-only hash can be initialized from a variable one:
.PP
.Vb 1
\&    Readonly::Hash %a => %computed_values;
.Ve
.PP
A read-only hash can be empty:
.PP
.Vb 3
\&    Readonly::Hash %a => ();
\&    # or
\&    Readonly::Hash %a;
.Ve
.PP
If you pass an odd number of values, the program will die:
.PP
.Vb 2
\&    Readonly::Hash my %a => (key1 => \*(Aqvalue1\*(Aq, "value2");
\&    # This dies with "Odd number of elements in hash assignment"
.Ve
.SH "ReadonlyX vs. Readonly"
.IX Header "ReadonlyX vs. Readonly"
The original Readonly module was written nearly twenty years ago when the
built-in capability to lock variables didn't exist in perl's core. The
original author came up with the amazingly brilliant idea to use the new (at
the time) \f(CW\*(C`tie(...)\*(C'\fR construct. It worked amazingly well! But it wasn't long
before the speed penalty of tied varibles became embarrassingly obvious. Check
any review of Readonly written before 2013; the main complaint was how slow it
was and the benchmarks proved it.
.PP
In an equally brilliant move to work around tie, Readonly::XS was released for
perl 5.8.9 and above. This bypassed \f(CW\*(C`tie(...)\*(C'\fR for basic scalars which made a
huge difference.
.PP
During all this, two very distinct APIs were also designed and supported by
Readonly. One for (then) modern perl and one written for perl 5.6. To make
this happen, time consuming eval operations were required and the codebase
grew so complex that fixing bugs was nearly impossible. Readonly was three
different modules all with different sets of quirks and bugs to fix depending
on what version of perl and what other modules you had installed. It was a
mess.
.PP
So, after the original author abandoned both Readonly and Readonly::XS, as
bugs were found, they went unfixed. The combination of speed and lack of
development spawned several similar modules which usually did a better job but
none were a total drop-in replacement.
.PP
Until now.
.PP
ReadonlyX is the best of recent versions of Readonly without the old \s-1API\s0 and
without the speed penalty of \f(CW\*(C`tie(...)\*(C'\fR. It's what I'd like to do with
Readonly if resolving bugs in it wouldn't break 16 years of code out there in
Darkpan.
.PP
In short, unlike Readonly, ReadonlyX...
.ie n .IP "...does not use slow ""tie(...)"" magic or eval. There shouldn't be a speed penalty after making the structure immutable. See the Benchmarks section below" 4
.el .IP "...does not use slow \f(CWtie(...)\fR magic or eval. There shouldn't be a speed penalty after making the structure immutable. See the Benchmarks section below" 4
.IX Item "...does not use slow tie(...) magic or eval. There shouldn't be a speed penalty after making the structure immutable. See the Benchmarks section below"
.PD 0
.IP "...does not strive to work on perl versions I can't even find a working build of to test against" 4
.IX Item "...does not strive to work on perl versions I can't even find a working build of to test against"
.IP "...has a single, clean \s-1API\s0! What do all of these different forms of the original Readonly \s-1API\s0 do?" 4
.IX Item "...has a single, clean API! What do all of these different forms of the original Readonly API do?"
.PD
.Vb 5
\&    use Readonly;
\&    Readonly  my @array1        => [2];
\&    Readonly \emy @array2        => [2];
\&    Readonly::Array  my @array3 => [2];
\&    Readonly::Array1 my @array4 => [2];
.Ve
.Sp
Be careful because they all behave very differently. Even your version of perl
and the contents of the list changes how they work. Give up? Yeah, me too.
Bonus: Guess which one doesn't actually make the list items read only.
.IP "...does the right thing when it comes to deep vs. shallow structures" 4
.IX Item "...does the right thing when it comes to deep vs. shallow structures"
.PD 0
.IP "...allows implicit undef values for scalars (Readonly inconsistantly allows this for hashes and arrays but not scalars)" 4
.IX Item "...allows implicit undef values for scalars (Readonly inconsistantly allows this for hashes and arrays but not scalars)"
.IP "...a lot more I can't think of right now but will add when they come to me" 4
.IX Item "...a lot more I can't think of right now but will add when they come to me"
.IP "...is around 100 lines instead of 460ish so maintaining it will be a breeze" 4
.IX Item "...is around 100 lines instead of 460ish so maintaining it will be a breeze"
.IP "...doesn't clobber predefined variables when making them readonly" 4
.IX Item "...doesn't clobber predefined variables when making them readonly"
.PD
Using Readonly, this:
.Sp
.Vb 3
\&    my @array = qw[very important stuff];
\&    Readonly::Array @array;
\&    print "@array";
.Ve
.Sp
\&...wouldn't print anything. I consider it a bug but I'm not sure why it was
designed this way originally. With ReadonlyX, you won't lose your
\&\f(CW\*(Aqvery important stuff\*(Aq\fR.
.Sp
Note that this is an incompatible change! If you attempt to do this and then
switch to plain 'ol Readonly, your code will not work.
.SH "Benchmarks"
.IX Header "Benchmarks"
Don't believe Readonly is slow? Here's the result of basic benchmarking:
.PP
.Vb 10
\&    Hash Benchmark: timing 5000000 iterations of const, normal, readonly, readonlyx...
\&         const:  3 wallclock secs ( 2.73 usr +  0.02 sys =  2.75 CPU) @ 1818181.82/s (n=5000000)
\&        normal:  3 wallclock secs ( 3.02 usr + \-0.02 sys =  3.00 CPU) @ 1666666.67/s (n=5000000)
\&      readonly: 47 wallclock secs (40.64 usr +  0.03 sys = 40.67 CPU) @ 122931.67/s (n=5000000)
\&     readonlyx:  4 wallclock secs ( 3.22 usr + \-0.01 sys =  3.20 CPU) @ 1560549.31/s (n=5000000)
\&    Array Benchmark: timing 5000000 iterations of const, normal, readonly, readonlyx...
\&         const:  3 wallclock secs ( 2.19 usr +  0.03 sys =  2.22 CPU) @ 2253267.24/s (n=5000000)
\&        normal:  1 wallclock secs ( 1.44 usr +  0.00 sys =  1.44 CPU) @ 3474635.16/s (n=5000000)
\&      readonly: 36 wallclock secs (32.52 usr +  0.13 sys = 32.64 CPU) @ 153181.58/s (n=5000000)
\&     readonlyx:  1 wallclock secs ( 1.12 usr + \-0.02 sys =  1.11 CPU) @ 4512635.38/s (n=5000000)
\&    Scalar Benchmark: timing 5000000 iterations of const, normal, readonly, readonlyx...
\&         const:  1 wallclock secs ( 1.14 usr + \-0.02 sys =  1.12 CPU) @ 4448398.58/s (n=5000000)
\&        normal:  1 wallclock secs ( 0.99 usr +  0.02 sys =  1.00 CPU) @ 4995005.00/s (n=5000000)
\&      readonly:  1 wallclock secs ( 1.25 usr +  0.00 sys =  1.25 CPU) @ 4000000.00/s (n=5000000)
\&     readonlyx:  2 wallclock secs ( 1.20 usr +  0.00 sys =  1.20 CPU) @ 4156275.98/s (n=5000000)
.Ve
.PP
Find the script to run them yourself in \f(CW\*(C`eg/benchmark.pl\*(C'\fR.
.SH "Requirements"
.IX Header "Requirements"
There are no non-core requirements.
.SH "Bug Reports"
.IX Header "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.
.PP
ReadonlyX can be found is the branch of Readonly found here:
https://github.com/sanko/readonly/tree/ReadonlyX
.SH "Author"
.IX Header "Author"
Sanko Robinson <sanko@cpan.org> \- http://sankorobinson.com/
.PP
\&\s-1CPAN ID: SANKO\s0
.SH "License and Legal"
.IX Header "License and Legal"
Copyright (C) 2016 by Sanko Robinson <sanko@cpan.org>
.PP
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.