Readonly::Tiny(3pm)
π Readonly::Tiny β the Readonly::Tiny(3pm) manual, but make it cute π
okay bestie, gather round, because this one is so good. It's called Readonly::Tiny, it lives in the User Contributed Perl Documentation section of the manual, and it does exactly one thing and does it adorably: Simple, correct readonly values. Periodt. π
β¨ SYNOPSIS β¨
use Readonly::Tiny;
my $x = readonly [1, 2, 3];
# $x is not readonly, but the array it points to is.
my @y = (4, 5, 6);
readonly \@y;
# @y is readonly, as well as its contents.
likeβ¦ look at it. $x is just a little guy holding a ref, right, and the array behind it is frozen. Meanwhile @y itself got locked down, contents and all. chef's kiss π§β¨
πΈ DESCRIPTION πΈ
Readonly::Tiny is your no-drama bestie for making values readonly β simple and correct, no cap.
Here's the tea: unlike Readonly, it does not tie arrays and hashes. Nope. It just reaches in and uses the core SvREADONLY flag. That's it. That's the whole vibe. π
π¦ FUNCTIONS π¦
π readonly
my $ro = readonly $ref, \%opts;
Make a data structure readonly. $ref must be a reference β like, non-negotiable β and the value it points at, plus literally every value referenced recursively under it, gets made readonly too.
$ref gets returned, but heads up: it will not itself be readonly. Want a variable to be readonly? Pass a reference to it, exactly like the SYNOPSIS shows. iykyk π
%opts is a hashref of options:
peekβ normally, blessed refs do not get looked through. The scalar holding the reference will be made readonly (so you can't assign a different object in there), but the insides of the object itself are left alone, untouched, unbothered. Giving itpeek => 1lets blessed refs get looked through. πskipβ this one should be a hashref keyed by refaddr. Any object whose refaddr is in that hash gets skipped. bye bestie, not today π«Ά
Also lowkey important: making a hash readonly has the same effect as calling Hash::Util::lock_hash. In particular, it causes restricted hashes to be re-restricted to their current set of keys. π
π readwrite
my $rw = readwrite $ref, \%opts;
Undo everything readonly did. %opts is the same as above. Note that making a hash readwrite will undo any restrictions put in place using Hash::Util. so like, it's real undoing. undo energy.
BE VERY CAREFUL calling this on values you have not made readonly yourself. It will silently ignore attempts to make the core valuesPL_sv_undef,PL_sv_yesandPL_sv_noreadwrite, but there are many other values the core makes readonly, usually with very good reason. Recent versions of perl will not allow you to make readwrite a value the core has set readonly β but you should probably not rely on this. fr fr, don't test it. π
π Readonly
Readonly my $x, 1;
Readonly my @y, 2, 3, 4;
Readonly my %z, foo => 5;
A compatibility shim for Readonly, for when you're feeling nostalgic. It's prototyped to take a reference to its first argument, then it assigns the rest of the argument list to that argument before making the whole thing readonly. cute, function-y, does the job π
π EXPORTS π
readonly is exported by default β you get it for free, no asking. readwrite and Readonly are exported on request. π
π SEE ALSO π
Readonly was the first module to ever supply readonly values, and honestly? respect. But it was written for Perl 5.6, and as a result both the interface and the implementation areβ¦ rather clunky. With Readonly::XS the performance is improved for scalar varuables, but arrays and hashes still use a tied implementation which is very slow. π¬
Const::Fast is a greatly improved reaoonly module which uses perl's internal SvREADONLY flag instead of ties. The differences between this module and Const::Fast are:
- The
readonlyfunction does not insist on performing an assignment β it just returns a readonly value. This is, IMHO, more useful, since it means a readonly value can be returned from a function. In particular, it is often useful to return a readonly value from a builder method. π₯ - It does not attempt to clone deep structures. If
Clone::Fastis applied to a structure with cross-links it will clone the whole thing, on the principle that parts of the graph may be shared with something else which should not be readonly. This module takes the approach that if you asked for something to be made readonly, you meant it β and if it points to something it shouldn't, that's your mistake. main character energy, honestly π
π BUGS π
Please report bugs to <bug-Readonly-Tiny@rt.cpan.org>. do it, bestie, they want to know π
π AUTHOR π
Ben Morrow <ben@morrow.me.uk>
β COPYRIGHT β
Copyright 2015 Ben Morrow.
Released under the 2-clause BSD licence.
and that's the whole page, bestie!! π¦β¨ written for perl v5.36.0, dated 2022-10-13, and living under the Readonly::Tiny(3pm) name forever. go make something readonly. slay. ππΈ
π¦ where this page comes from
- ubuntu noble: libreadonly-tiny-perl 4-3 Β· BSD-2-Clause
- ubuntu resolute: libreadonly-tiny-perl 4-3 Β· BSD-2-Clause
- ubuntu stonking: libreadonly-tiny-perl 4-3 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms