Build.PL000644000000000000 45512743143001 15225 0ustar00unknownunknown000000000000ReadonlyX-1.04# ========================================================================= # THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. # DO NOT EDIT DIRECTLY. # ========================================================================= use 5.008_001; use strict; use Module::Build::Tiny 0.035; Build_PL(); Changes000644000000000000 122112743143001 15234 0ustar00unknownunknown000000000000ReadonlyX-1.04Revision history for Perl extension ReadonlyX. 1.04 2016-07-18T12:04:41Z - Fake $Readonly::VERSION 1.03 2016-07-16T21:39:43Z - Don't clobber predefined variables with implicit undef - More documentation updates - Wrap tests that could fail due to bugs in perl related to readonly arrays - Include Readonly vs. ReadonlyX benchmarks in docs and update eg/benchmark.pl 1.02 2016-07-16T12:40:12Z - Minor documentation update 1.01 2016-07-16T01:41:17Z - Hide fake Readonly package from PAUSE (I hope) 1.00 2016-07-16T01:29:19Z - Brand spankin' new! - This passes a modernized variation of Readonly's 2.05 test suite LICENSE000644000000000000 4374212743143001 15004 0ustar00unknownunknown000000000000ReadonlyX-1.04This software is copyright (c) 2013 by Sanko Robinson . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2013 by Sanko Robinson . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2013 by tokuhirom . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The EndMETA.json000644000000000000 402312743143001 15365 0ustar00unknownunknown000000000000ReadonlyX-1.04{ "abstract" : "Faster facility for creating read-only scalars, arrays, hashes", "author" : [ "Sanko Robinson - http://sankorobinson.com/" ], "dynamic_config" : 0, "generated_by" : "Minilla/v3.0.2", "license" : [ "artistic_2" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "ReadonlyX", "no_index" : { "directory" : [ "t", "xt", "inc", "share", "eg", "examples", "author", "builder" ] }, "prereqs" : { "configure" : { "requires" : { "Module::Build::Tiny" : "0.035" } }, "develop" : { "requires" : { "Test::CPAN::Meta" : "0", "Test::MinimumVersion::Fast" : "0.04", "Test::PAUSE::Permissions" : "0.04", "Test::Pod" : "1.41", "Test::Spellunker" : "v0.2.7" } }, "runtime" : { "requires" : { "perl" : "5.008" } }, "test" : { "requires" : { "Test::Fatal" : "0", "Test::More" : "0" } } }, "provides" : { "ReadonlyX" : { "file" : "lib/ReadonlyX.pm", "version" : "1.04" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/sanko/readonly/issues" }, "homepage" : "https://github.com/sanko/readonly", "repository" : { "url" : "git://github.com/sanko/readonly.git", "web" : "https://github.com/sanko/readonly" } }, "version" : "1.04", "x_contributors" : [ "David Steinbrunner ", "Peter Valdemar Mørch ", "vti ", "Ronald Schmidt ", "Michael Ivanchenko " ], "x_serialization_backend" : "JSON::PP version 2.27400" } README.md000644000000000000 3012612743143001 15246 0ustar00unknownunknown000000000000ReadonlyX-1.04[![Build Status](https://travis-ci.org/sanko/readonly.svg?branch=master)](https://travis-ci.org/sanko/readonly) # NAME ReadonlyX - Faster facility for creating read-only scalars, arrays, hashes # Synopsis 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 => 'Welcome'; 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 => 'value', key2 => 'value'); Readonly::Hash my %hash2 => (key => 'value', key2 => 'value'); # or: Readonly::Hash my %hash3 => {key => 'value', key2 => 'value'}; # You can use the read-only variables like any regular variables: print $sca1; my $something = $sca1 + $arr1[2]; warn 'Blah!' 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, 'seven'; # "Modification of a read-only value attempted" $arr1[1] = 'nine'; # "Modification of a read-only value attempted" delete $hash1{key}; # Attempt to delete readonly key 'key' from a restricted hash # Create mutable clones Readonly::Scalar $scalar => {qw[this that]}; # $scalar->{'eh'} = 'foo'; # Modification of a read-only value attempted my $scalar_clone = Readonly::Clone $scalar; $scalar_clone->{'eh'} = 'foo'; # $scalar_clone is now {this => 'that', eh => 'foo'}; # Description This is a near-drop-in replacement for [Readonly](https://metacpan.org/pod/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. 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 ["ReadonlyX vs. Readonly"](#readonlyx-vs-readonly) for more. # Functions All of these functions can be imported into your package by name. ## Readonly::Scalar Readonly::Scalar $pi => 3.14; Readonly::Scalar my $aref => [qw[this that]]; # list ref Readonly::Scalar my $href => {qw[this that]}; # hash ref 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. 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. If the variable is already readonly, the program will die with an error about reassigning readonly variables. ## Readonly::Array Readonly::Array @arr1 => [1 .. 4]; Readonly::Array my @arr2 => (1, 3, 5, 7, 9); 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. 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. If the variable is already readonly, the program will die with an error about reassigning readonly variables. ## Readonly::Hash Readonly::Hash %h => (key => 'value', key2 => 'value'); Readonly::Hash %h => {key => 'value', key2 => 'value'}; 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. 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. If the variable is already readonly, the program will die with an error about reassigning readonly variables. ## Readonly::Clone my $scalar_clone = Readonly::Clone $scalar; When cloning using [Storable](https://metacpan.org/pod/Storable) or [Clone](https://metacpan.org/pod/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. Readonly::Scalar my $scalar => {qw[this that]}; # $scalar->{'eh'} = 'foo'; # Modification of a read-only value attempted my $scalar_clone = Readonly::Clone $scalar; $scalar_clone->{'eh'} = 'foo'; # $scalar_clone is now {this => 'that', eh => 'foo'}; In this example, the new variable (`$scalar_clone`) is a mutable clone of the original `$scalar`. You can change it like any other variable. # Examples Here are a few very simple examples again to get you started: ## 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; Need an undef constant? Okay: Readonly::Scalar $a; ## 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 => (); # or Readonly::Array @a; ## Hashes Typical usage: Readonly::Hash %a => (key1 => 'value1', key2 => 'value2'); # or 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 => (); # or Readonly::Hash %a; If you pass an odd number of values, the program will die: Readonly::Hash my %a => (key1 => 'value1', "value2"); # This dies with "Odd number of elements in hash assignment" # 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) `tie(...)` 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. In an equally brilliant move to work around tie, Readonly::XS was released for perl 5.8.9 and above. This bypassed `tie(...)` for basic scalars which made a huge difference. 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. 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. Until now. ReadonlyX is the best of recent versions of Readonly without the old API and without the speed penalty of `tie(...)`. 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. In short, unlike Readonly, ReadonlyX... - ...does not use slow `tie(...)` magic or eval. There shouldn't be a speed penalty after making the structure immutable. See the [Benchmarks](https://metacpan.org/pod/Benchmarks) section below - ...does not strive to work on perl versions I can't even find a working build of to test against - ...has a single, clean API! What do all of these different forms of the original Readonly API do? use Readonly; Readonly my @array1 => [2]; Readonly \my @array2 => [2]; Readonly::Array my @array3 => [2]; Readonly::Array1 my @array4 => [2]; 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. - ...does the right thing when it comes to deep vs. shallow structures - ...allows implicit undef values for scalars (Readonly inconsistantly allows this for hashes and arrays but not scalars) - ...a lot more I can't think of right now but will add when they come to me - ...is around 100 lines instead of 460ish so maintaining it will be a breeze - ...doesn't clobber predefined variables when making them readonly Using Readonly, this: my @array = qw[very important stuff]; Readonly::Array @array; print "@array"; ...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 `'very important stuff'`. 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. # Benchmarks Don't believe Readonly is slow? Here's the result of basic benchmarking: 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) Find the script to run them yourself in `eg/benchmark.pl`. # Requirements There are no non-core requirements. # Bug Reports If email is better for you, [my address is mentioned below](#author) but I would rather have bugs sent through the issue tracker found at http://github.com/sanko/readonly/issues. ReadonlyX can be found is the branch of Readonly found here: https://github.com/sanko/readonly/tree/ReadonlyX # Author Sanko Robinson - http://sankorobinson.com/ CPAN ID: SANKO # License and Legal Copyright (C) 2016 by Sanko Robinson This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. cpanfile000644000000000000 7112743143001 15407 0ustar00unknownunknown000000000000ReadonlyX-1.04test_requires 'Test::More'; test_requires 'Test::Fatal'; benchmark.pl000644000000000000 465512743143001 16641 0ustar00unknownunknown000000000000ReadonlyX-1.04/eguse strict; use warnings; use lib '../lib'; use Benchmark; my $scalar; { package constant; use constant CONST_SCALAR => 'Fourscore and seven years ago...'; use constant CONST_HASH => {key => 'value'}; use constant CONST_ARRAY => qw[dog cat bird fish]; sub scalar { $scalar = CONST_SCALAR; } sub hash { $scalar = CONST_HASH->{key}; } sub array { $scalar = (CONST_ARRAY)[1]; } } { package normal; my $normal_scalar = 'Fourscore and seven years ago...'; my %normal_hash = (key => 'value'); my @normal_array = (qw[dog cat bird fish]); sub scalar { $scalar = $normal_scalar; } sub hash { $scalar = $normal_hash{key}; } sub array { $scalar = $normal_array[1]; } } { package readonly; use namespace::clean; my ($normal_scalar, %normal_hash, @normal_array); eval <<'END'; use Readonly; Readonly::Scalar $normal_scalar => 'Fourscore and seven years ago...'; Readonly::Hash %normal_hash => {key => 'value'}; Readonly::Array @normal_array => qw[dog cat bird fish]; END sub scalar { $scalar = $normal_scalar; } sub hash { $scalar = $normal_hash{key}; } sub array { $scalar = $normal_array[1]; } } { package readonlyx; my ($normal_scalar, %normal_hash, @normal_array); eval <<'END'; use ReadonlyX; Readonly::Scalar $normal_scalar => 'Fourscore and seven years ago...'; Readonly::Hash %normal_hash => {key => 'value'}; Readonly::Array @normal_array => qw[dog cat bird fish]; END sub scalar { $scalar = $normal_scalar; } sub hash { $scalar = $normal_hash{key}; } sub array { $scalar = $normal_array[1]; } } # my %tests = (scalar => {const => \&constant::scalar, normal => \&normal::scalar, readonlyx => \&readonlyx::scalar, readonly => \&readonly::scalar }, hash => {const => \&constant::hash, normal => \&normal::hash, readonlyx => \&readonlyx::hash, readonly => \&readonly::hash }, array => {const => \&constant::array, normal => \&normal::array, readonlyx => \&readonlyx::array, readonly => \&readonly::array } ); # for my $type (keys %tests) { print ucfirst $type . ' '; timethese(5_000_000, $tests{$type}); } ReadonlyX.pm000644000000000000 3641212743143001 17004 0ustar00unknownunknown000000000000ReadonlyX-1.04/libpackage ReadonlyX; use 5.008; use strict; use warnings; our $VERSION = "1.04"; BEGIN { *ReadonlyX:: = *Readonly:: } package # hide from PAUSE Readonly; # I wish... use Carp; use Exporter; use vars qw/@ISA @EXPORT @EXPORT_OK/; push @ISA, 'Exporter'; push @EXPORT, qw/Readonly/; push @EXPORT_OK, qw/Scalar Array Hash/; our $VERSION = "1.04"; # Fake # sub Array(\@;@); sub Hash(\%;@); sub Scalar($;$); sub Readonly(\[%@$]$); # sub Array(\@;@) { my $var = $#_ == 0 && defined $_[0] ? $_[0] : $_[1]; @{$_[0]} = ref $var eq 'ARRAY' && $#_ == 1 && ref $var eq 'ARRAY' ? @{$var} : @_[1 .. $#_] if $#_ > 0; _readonly($_[0]); } sub Hash(\%;@) { my $var = $#_ == 0 && defined $_[0] ? $_[0] : $_[1]; my $ref = ref $var; Carp::croak 'Odd number of elements in hash assignment' unless (@_ % 2 == 1) || $ref eq 'HASH'; %{$_[0]} = ref $var eq 'HASH' && $#_ == 1 ? %{$var} : @_[1 .. $#_] if $#_ > 0; _readonly($_[0]); } sub Scalar($;$) { my $var = $#_ == 0 && defined $_[0] ? $_[0] : $_[1]; my $ref = ref $var; $ref eq 'ARRAY' ? $_[0] = $var : $ref eq 'HASH' ? $_[0] = $var : $ref eq 'SCALAR' or $ref eq '' ? $_[0] = $var : $ref eq 'REF' ? $_[0] = \$_[1] : 1; _readonly($_[0]); Internals::SvREADONLY($_[0], 1); } sub Readonly(\[%@$]$) { my $type = ref $_[0]; return Scalar(${$_[0]}, defined $_[1] ? $_[1] : ()) if $type eq 'SCALAR' or $type eq ''; return Hash(%{$_[0]}, defined $_[1] ? $_[1] : ()) if $type eq 'HASH'; return Array(@{$_[0]}, defined $_[1] ? $_[1] : []) if $type eq 'ARRAY'; } sub _readonly { my $type = ref $_[0]; my ($onoff) = $#_ ? $_[1] : 1; if ($type eq '') { return Internals::SvREADONLY($_[0], $onoff); } elsif ($type eq 'SCALAR') { return Internals::SvREADONLY(${$_[0]}, $onoff); } elsif ($type eq 'HASH') { for my $key (keys %{$_[0]}) { _readonly($_[0]->{$key}, $onoff); Internals::SvREADONLY($_[0]->{$key}, $onoff); } return Internals::SvREADONLY(%{$_[0]}, $onoff); } elsif ($type eq 'ARRAY') { for my $index (0 .. $#{$_[0]}) { _readonly($_[0]->[$index], $onoff); Internals::SvREADONLY($_[0]->[$index], $onoff); } return Internals::SvREADONLY(@{$_[0]}, $onoff); } elsif ($type eq 'REF') { my $refref = ref ${$_[0]}; _readonly(${$_[0]}, $onoff); return Internals::SvREADONLY(@${$_[0]}, $onoff) if $refref eq 'ARRAY'; return Internals::SvREADONLY(%${$_[0]}, $onoff) if $refref eq 'HASH'; return Internals::SvREADONLY(${$_[0]}, $onoff); } Carp::carp 'We do not know what to do with ' . $type; } sub Clone(\[$@%]) { require Storable; my $retval = Storable::dclone($_[0]); $retval = $$retval if ref $retval eq 'REF'; my $type = ref $retval; _readonly(( $type eq 'SCALAR' || $type eq '' ? $$retval : $type eq 'HASH' ? $retval : $type eq 'ARRAY' ? @$retval : $retval ), 0 ); return $type eq 'SCALAR' ? $$retval : ($type eq 'ARRAY' ? wantarray ? @$retval : $retval : ($type eq 'HASH' ? wantarray ? %$retval : $retval : $retval) ); } 1; =head1 NAME ReadonlyX - Faster facility for creating read-only scalars, arrays, hashes =head1 Synopsis 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 => 'Welcome'; 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 => 'value', key2 => 'value'); Readonly::Hash my %hash2 => (key => 'value', key2 => 'value'); # or: Readonly::Hash my %hash3 => {key => 'value', key2 => 'value'}; # You can use the read-only variables like any regular variables: print $sca1; my $something = $sca1 + $arr1[2]; warn 'Blah!' 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, 'seven'; # "Modification of a read-only value attempted" $arr1[1] = 'nine'; # "Modification of a read-only value attempted" delete $hash1{key}; # Attempt to delete readonly key 'key' from a restricted hash # Create mutable clones Readonly::Scalar $scalar => {qw[this that]}; # $scalar->{'eh'} = 'foo'; # Modification of a read-only value attempted my $scalar_clone = Readonly::Clone $scalar; $scalar_clone->{'eh'} = 'foo'; # $scalar_clone is now {this => 'that', eh => 'foo'}; =head1 Description This is a near-drop-in replacement for L, 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. 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 for more. =head1 Functions All of these functions can be imported into your package by name. =head2 Readonly::Scalar Readonly::Scalar $pi => 3.14; Readonly::Scalar my $aref => [qw[this that]]; # list ref Readonly::Scalar my $href => {qw[this that]}; # hash ref 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. 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. If the variable is already readonly, the program will die with an error about reassigning readonly variables. =head2 Readonly::Array Readonly::Array @arr1 => [1 .. 4]; Readonly::Array my @arr2 => (1, 3, 5, 7, 9); 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. 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. If the variable is already readonly, the program will die with an error about reassigning readonly variables. =head2 Readonly::Hash Readonly::Hash %h => (key => 'value', key2 => 'value'); Readonly::Hash %h => {key => 'value', key2 => 'value'}; 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. 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. If the variable is already readonly, the program will die with an error about reassigning readonly variables. =head2 Readonly::Clone my $scalar_clone = Readonly::Clone $scalar; When cloning using L or L 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. Readonly::Scalar my $scalar => {qw[this that]}; # $scalar->{'eh'} = 'foo'; # Modification of a read-only value attempted my $scalar_clone = Readonly::Clone $scalar; $scalar_clone->{'eh'} = 'foo'; # $scalar_clone is now {this => 'that', eh => 'foo'}; In this example, the new variable (C<$scalar_clone>) is a mutable clone of the original C<$scalar>. You can change it like any other variable. =head1 Examples Here are a few very simple examples again to get you started: =head2 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; Need an undef constant? Okay: Readonly::Scalar $a; =head2 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 => (); # or Readonly::Array @a; =head2 Hashes Typical usage: Readonly::Hash %a => (key1 => 'value1', key2 => 'value2'); # or 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 => (); # or Readonly::Hash %a; If you pass an odd number of values, the program will die: Readonly::Hash my %a => (key1 => 'value1', "value2"); # This dies with "Odd number of elements in hash assignment" =head1 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) C 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. In an equally brilliant move to work around tie, Readonly::XS was released for perl 5.8.9 and above. This bypassed C for basic scalars which made a huge difference. 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. 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. Until now. ReadonlyX is the best of recent versions of Readonly without the old API and without the speed penalty of C. 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. In short, unlike Readonly, ReadonlyX... =over =item ...does not use slow C magic or eval. There shouldn't be a speed penalty after making the structure immutable. See the L section below =item ...does not strive to work on perl versions I can't even find a working build of to test against =item ...has a single, clean API! What do all of these different forms of the original Readonly API do? use Readonly; Readonly my @array1 => [2]; Readonly \my @array2 => [2]; Readonly::Array my @array3 => [2]; Readonly::Array1 my @array4 => [2]; 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. =item ...does the right thing when it comes to deep vs. shallow structures =item ...allows implicit undef values for scalars (Readonly inconsistantly allows this for hashes and arrays but not scalars) =item ...a lot more I can't think of right now but will add when they come to me =item ...is around 100 lines instead of 460ish so maintaining it will be a breeze =item ...doesn't clobber predefined variables when making them readonly Using Readonly, this: my @array = qw[very important stuff]; Readonly::Array @array; print "@array"; ...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 C<'very important stuff'>. 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. =back =head1 Benchmarks Don't believe Readonly is slow? Here's the result of basic benchmarking: 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) Find the script to run them yourself in C. =head1 Requirements There are no non-core requirements. =head1 Bug Reports If email is better for you, L but I would rather have bugs sent through the issue tracker found at http://github.com/sanko/readonly/issues. ReadonlyX can be found is the branch of Readonly found here: https://github.com/sanko/readonly/tree/ReadonlyX =head1 Author Sanko Robinson - http://sankorobinson.com/ CPAN ID: SANKO =head1 License and Legal Copyright (C) 2016 by Sanko Robinson This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut minil.toml000644000000000000 27012743143001 15731 0ustar00unknownunknown000000000000ReadonlyX-1.04name = "ReadonlyX" badges = ["travis"] module_maker="ModuleBuildTiny" license="artistic_2" [release] pause_config = "C:/Strawberry/pause.upload" [ReleaseTest] MinimumVersion = false 007_implicit_undef.t000644000000000000 106012743143001 20713 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/bugs#!perl -I../../lib # No implicit undef! use strict; use Test::More; use ReadonlyX; # Readonly::Scalar my $scalar; is_deeply [$scalar], [undef], 'Readonly::Scalar allows implicit undef values'; # Readonly::Hash my %hash; is_deeply [%hash], [], 'Readonly::Hash allows implicit undef values'; # Readonly::Array my @array; is_deeply [@array], [], 'Readonly::Array allows implicit undef values'; # eval q'Readonly my $scalar;'; like $@ => qr[Not enough arguments for Readonly::Readonly], 'Readonly::Readonly does not allow implicit undef values'; done_testing; 025_partial_clone.t000644000000000000 77512743143001 20530 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/bugs#!perl -I../../lib # Verify partial clones of Readonly vars are mutable use strict; use Test::More; use ReadonlyX; use Test::Fatal; # Readonly::Scalar our $MAP => {'record' => {id => 1, title => 'Record', } }; my $map_partial_copy = Readonly::Clone $MAP->{record}; is exception { $map_partial_copy->{id} = 42; }, undef, 'create mutalbe copy'; # is $map_partial_copy->{id}, 42, 'mutable copy is... well, mutable'; # done_testing; array.t000644000000000000 426112743143001 17133 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Readonly array tests use strict; use warnings; use Test::More; use ReadonlyX; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n"; } use vars qw/@a1 @a2/; my @ma1; # creation (3 tests) eval 'Readonly::Array @a1;'; is $@ => '', 'Create empty global array'; eval 'Readonly::Array @ma1 => ();'; is $@ => '', 'Create empty lexical array'; eval 'Readonly::Array @a2 => (1,2,3,4,5);'; is $@ => '', 'Create global array'; # fetching (3 tests) ok !defined($a1[0]), 'Fetch global'; is $a2[0] => 1, 'Fetch global'; is $a2[-1] => 5, 'Fetch global'; # fetch size (3 tests) is scalar(@a1) => 0, 'Global size (zero)'; is scalar(@ma1) => 0, 'Lexical size (zero)'; is $#a2 => 4, 'Global last element (nonzero)'; # store (2 tests) TODO: { local $TODO = 'perl *could* mess with some readonly vars before 5.12 stable'; eval { $ma1[0] = 5; }; is $@ => expected(__LINE__- 1), 'Lexical store'; } eval { $a2[3] = 4; }; is $@ => expected(__LINE__- 1), 'Global store'; # storesize (1 test) eval { $#a1 = 15; }; is $@ => expected(__LINE__- 1), 'Change size'; # extend (1 test) eval { $a1[77] = 88; }; is $@ => expected(__LINE__- 1), 'Extend'; # exists (2 tests) SKIP: { skip "Can't do exists on array until Perl 5.6", 2 if $] < 5.006; eval 'ok(exists $a2[4], "Global exists")'; eval 'ok(!exists $ma1[4], "Lexical exists")'; } # clear (1 test) eval { @a1 = (); }; is $@ => expected(__LINE__- 1), 'clear'; TODO: { local $TODO = 'perl *could* mess with some readonly vars before 5.12 stable'; # push (1 test) eval { push @ma1, -1; }; is $@ => expected(__LINE__- 1), 'push'; } # unshift (1 test) eval { unshift @a2, -1; }; is $@ => expected(__LINE__- 1), 'unshift'; # pop (1 test) eval { pop(@a2); }; is $@ => expected(__LINE__- 1), 'pop'; # shift (1 test) eval { shift(@a2); }; is $@ => expected(__LINE__- 1), 'shift'; TODO: { local $TODO = "Can't test splice on readonly array; bug in perl"; # splice (1 test) eval { splice @a2, 0, 1; }; is $@ => expected(__LINE__- 1), 'splice'; } done_testing; clone.t000644000000000000 364512743143001 17122 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general# Readonly clone tests use strict; use warnings; use Test::More; use lib '../../lib'; use ReadonlyX; # Readonly::Scalar my $scalar => 13; Readonly::Array my @array => (1, 2, 3); Readonly::Hash my %hash => (foo => 'bar'); Readonly::Array my @deep_array => (1, \@array); Readonly::Hash my %deep_hash => (foo => \@array); # my $scalar_clone = Readonly::Clone $scalar; $scalar_clone++; is $scalar_clone, 14, 'scalar clone is mutable'; # my @array_clone = Readonly::Clone @array; $array_clone[1] = 4; is $array_clone[1], 4, 'array clone is mutable'; # my %hash_clone = Readonly::Clone %hash; $hash_clone{foo} = 'baz'; is $hash_clone{foo}, 'baz', 'hash clone is mutable'; # my @deep_array_clone = Readonly::Clone @deep_array; $deep_array_clone[1]->[2] = 4; is $deep_array_clone[1]->[2], 4, 'deep array clone is mutable'; # my %deep_hash_clone = Readonly::Clone %deep_hash; $deep_hash_clone{foo}->[1] = 4; is $deep_hash_clone{foo}->[1], 4, 'deep hash clone is mutable'; # } { Readonly::Scalar my $scalar => ['string']; my $scalar_clone = Readonly::Clone $scalar; push @$scalar_clone, 'foo'; is_deeply $scalar_clone, [qw[string foo]], 'listref clone is mutable'; } { Readonly::Scalar my $scalar => ['string']; my @scalar_clone = Readonly::Clone $scalar; push @scalar_clone, 'foo'; is_deeply \@scalar_clone, [qw[string foo]], 'listref clone is mutable (take two)'; } { Readonly::Scalar my $scalar => {qw[this that]}; my $scalar_clone = Readonly::Clone $scalar; $scalar_clone->{'eh'} = 'foo'; is_deeply $scalar_clone, {this => 'that', eh => 'foo'}, 'hashref clone is mutable'; # my %scalar_clone = Readonly::Clone $scalar; $scalar_clone{'eh'} = 'foo'; is_deeply [\%scalar_clone], [{this => 'that', eh => 'foo'}], 'hashref clone is mutable (take two)'; } # done_testing; deepa.t000644000000000000 143212743143001 17070 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Test Array vs Array1 functionality use strict; use Test::More; use ReadonlyX; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n"; } use vars qw/@a1 /; my $m1 = 17; # Create (2 tests) eval { Readonly::Array @a1 => (\$m1, {x => 5, z => [1, 2, 3]}) }; is $@ => '', 'Create a deep reference array'; # Modify (10 tests) eval { $a1[0] = 7; }; is $@ => expected(__LINE__- 1), 'Modify a1'; eval { ${$a1[0]} = "the"; }; is $@ => expected(__LINE__- 1), 'Deep-modify a1'; is $m1 => 17, 'a1 unchanged'; eval { $a1[1]{z}[1] = 42; }; is $@ => expected(__LINE__- 1), 'Deep-deep modify a1'; is $a1[1]{z}[1] => 2, 'a1 unchanged'; # done_testing; deeph.t000644000000000000 144312743143001 17101 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Test Hash vs Hash1 functionality use strict; use Test::More; use ReadonlyX; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n"; } use vars qw/%h1 /; my $m1 = 17; # Create (2 tests) eval {Readonly::Hash %h1 => (key1 => \$m1, key2 => {x => 5, z => [1, 2, 3]})}; is $@ => '', 'Create a deep reference array'; # Modify (10 tests) eval {$h1{key1} = 7}; is $@ => expected(__LINE__-1), 'Modify h1'; eval {${$h1{key1}} = "the"}; is $@ => expected(__LINE__-1), 'Deep-modify h1'; is $m1 => 17, 'h1 unchanged'; eval {$h1{key2}{z}[1] = 42}; is $@ => expected(__LINE__-1), 'Deep-deep modify h1'; is $h1{key2}{z}[1] => 2, 'h1 unchanged'; # done_testing; deeps.t000644000000000000 216112743143001 17112 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Test Scalar vs Scalar1 functionality use strict; use Test::More; use ReadonlyX; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n"; } use vars qw/$s2 $s4/; my $m1 = 17; my $m2 = \$m1; # Create (4 tests) eval {Readonly::Scalar $s2 => ["this", "is", "a", "test", {x => 5}]}; is $@ => '', 'Create a deep reference scalar'; eval {Readonly::Scalar $s4 => $m2}; is $@ => '', 'Create a deep scalar ref'; # Modify (16 tests) eval {$s2 = 7}; is $@ => expected(__LINE__-1), 'Modify s2'; eval {$s4 = 7}; is $@ => expected(__LINE__-1), 'Modify s4'; eval {$s2->[2] = "the"}; is $@ => expected(__LINE__-1), 'Deep-modify s2'; is $s2->[2] => 'a', 's2 modification supposed to fail'; eval {$s2->[4]{z} = 42}; like $@ => qr[Attempt to access disallowed key 'z' in a restricted hash], 'Deep-deep modify s2'; ok !exists($s2->[4]{z}), 's2 mod supposed to fail'; eval {$$s4 = 21}; is $@ => expected(__LINE__-1), 'Deep-modify s4 should fail'; is $m1 => 17, 's4 mod should fail'; # done_testing; docs.t000644000000000000 367312743143001 16753 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Examples from the docs -- make sure they work! use strict; use Test::More; use ReadonlyX; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n"; } my ($a1, $a2, @a1, @a2, @a3, @a4, @a5, @a6, %a1, %a2, %a3, %a4, %a5); eval { Readonly::Scalar $a1 => "A string value"; }; is $@ => '', 'A string value'; my $computed_value = 5 + 5; eval { Readonly::Scalar $a2 => $computed_value; }; is $@ => '', 'Scalar computed value'; eval { Readonly::Array @a1 => (1, 2, 3, 4) }; is $@ => '', 'Array, with parens'; eval { Readonly::Array @a2 => 1, 2, 3, 4 }; is $@ => '', 'Array, without parens'; eval { Readonly::Array @a3 => qw/1 2 3 4/ }; is $@ => '', 'Array, with qw'; my @computed_values = qw/a b c d e f/; eval { Readonly::Array @a4 => @computed_values }; is $@ => '', 'Array, with computed values'; eval { Readonly::Array @a5 => () }; is $@ => '', 'Empty array 1'; eval { Readonly::Array @a6 }; is $@ => '', 'Empty array 2'; eval { Readonly::Hash %a1 => (key1 => "value1", key2 => "value2") }; is $@ => '', 'Hash constant'; my %computed_values = qw/a A b B c C d D/; eval { Readonly::Hash %a2 => %computed_values }; is $@ => '', 'Hash, computed values'; eval { Readonly::Hash %a3 => () }; is $@ => '', 'Empty hash 1'; eval { Readonly::Hash %a4 }; is $@ => '', 'Empty hash 2'; eval { Readonly::Hash %a5 => (key1 => "value1", "value2") }; like $@, qr/Odd number of elements in hash assignment/, 'Odd number of values'; use vars qw/@deep/; eval { Readonly::Array @deep => (1, 2, {perl => "Rules", java => "Bites"}, 4, 5); }; eval { $deep[1] = 7 }; is $@ => expected(__LINE__- 1), 'deep test 3'; is $deep[1] => 2, 'deep test 3 confirm'; eval { $deep[2]{APL} = "Weird" }; like $@ => qr[Attempt to access disallowed key 'APL' in a restricted hash], 'deep test 4'; ok !exists($deep[2]{APL}), 'deep test 4 confirm'; # done_testing; export.t000644000000000000 33312743143001 17312 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib use strict; use Test::More; # use_ok('ReadonlyX', qw/Readonly Scalar Array Hash/); # can_ok 'main', 'Readonly'; can_ok 'main', 'Scalar'; can_ok 'main', 'Array'; can_ok 'main', 'Hash'; # done_testing; hash.t000644000000000000 341712743143001 16742 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Readonly hash tests use strict; use Test::More; use Test::Fatal; use ReadonlyX; # sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n"; } use vars qw/%h1/; my (%mh1, %mh2); # creation (3 tests) eval { Readonly::Hash %h1 => (a => "A", b => "B", c => "C", d => "D") }; is $@ => '', 'Create global hash'; # Scalar like(exception { Readonly::Hash %mh1 => (one => 1, two => 2, three => 3, 4) }, qr'Odd number of elements in hash assignment', 'odd number of elements' ); eval { Readonly::Hash %mh1 => {one => 1, two => 2, three => 3, four => 4} }; is $@ => '', 'Create lexical hash'; # fetch (3 tests) is $h1{a} => 'A', 'Fetch global'; like(exception { defined $h1{'q'}}, qr[Attempt to access disallowed key 'q' in a restricted hash], 'nonexistent element undefined and cannot be autoinstantiated' ); is $mh1{two} => 2, 'Fetch lexical'; # store (1 test) eval { $h1{a} = 'Z' }; is $@ => expected(__LINE__- 1), 'Store'; # delete (1 test) eval { delete $h1{c} }; like $@,qr[Attempt to delete readonly key 'c' from a restricted hash], 'delete'; # clear (1 test) eval { %h1 = () }; like $@,qr[Attempt to delete readonly key '.' from a restricted hash], 'clear'; # exists (3 tests) ok exists $h1{a}, 'Exists'; eval { ok !exists $h1{x}, "Doesn't exist" }; is $@ => '', "Doesn't exist (no error)"; # keys, values (4 tests) my @a = sort keys %h1; is $a[0], 'a', 'Keys a'; is $a[1], 'b', 'Keys b'; @a = sort values %h1; is $a[0], 'A', 'Values A'; is $a[1], 'B', 'Values B'; # each (2 tests) my ($k, $v); while (($k, $v) = each %h1) { $mh2{$k} = $v; } is $mh2{c} => 'C', 'Each C'; is $mh2{d} => 'D', 'Each D'; # done_testing; reassign.t000644000000000000 605612743143001 17634 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Readonly reassignment-prevention tests use strict; use Test::More; use ReadonlyX; use vars qw($s1 @a1 %h1 $s2 @a2 %h2); Readonly::Scalar $s1 => 'a scalar value'; Readonly::Array @a1 => 'an', 'array', 'value'; Readonly::Hash %h1 => {a => 'hash', of => 'things'}; my $err = qr/^Modification of a read-only value attempted/; # Reassign scalar eval {Readonly::Scalar $s1 => "a second scalar value"}; like $@ => $err, 'Readonly::Scalar reassign die'; is $s1 => 'a scalar value', 'Readonly::Scalar reassign no effect'; # Reassign array eval {Readonly::Array @a1 => "another", "array"}; like $@ => $err, 'Readonly::Array reassign die'; ok eq_array(\@a1, [qw[an array value]]) => 'Readonly::Array reassign no effect'; # Reassign hash eval {Readonly::Hash %h1 => "another", "hash"}; like $@ => qr[Attempt to delete readonly key '.+' from a restricted hash], 'Readonly::Hash reassign die'; ok eq_hash(\%h1, {a => 'hash', of => 'things'}) => 'Readonly::Hash reassign no effect'; # Now use the naked Readonly function SKIP: { skip 'Readonly \\ syntax is for perls earlier than 5.8', 7 if $] >= 5.008; eval q{ Readonly \$s2 => 'another scalar value'; Readonly \@a2 => 'another', 'array', 'value'; Readonly \%h2 => {another => 'hash', of => 'things'}; }; # Reassign scalar eval q{Readonly \$s2 => "something bad!"}; like $@ => $err, 'Readonly Scalar reassign die'; is $s2 => 'another scalar value', 'Readonly Scalar reassign no effect'; # Reassign array eval q{Readonly \@a2 => "something", "bad", "!"}; like $@ => $err, 'Readonly Array reassign die'; ok eq_array(\@a2, [qw[another array value]]) => 'Readonly Array reassign no effect'; # Reassign hash eval q{Readonly \%h2 => {another => "bad", hash => "!"}}; like $@ => $err, 'Readonly Hash reassign die'; ok eq_hash(\%h2, {another => 'hash', of => 'things'}) => 'Readonly Hash reassign no effect'; # Reassign real constant eval q{Readonly \"scalar" => "vector"}; like $@ => qr/^Modification of a read-only value attempted at \(eval \d+\),? line 1/, 'Reassign indirect via ref'; }; SKIP: { skip 'Readonly $@% syntax is for perl 5.8 or later', 6 unless $] >= 5.008; Readonly $s2 => 'another scalar value'; Readonly @a2 => ['another', 'array', 'value']; Readonly %h2 => {another => 'hash', of => 'things'}; # Reassign scalar eval q{Readonly $s2 => "something bad!"}; like $@ => $err, 'Readonly Scalar reassign die'; is $s2 => 'another scalar value', 'Readonly Scalar reassign no effect'; # Reassign array eval q{Readonly @a2 => "something", "bad", "!"}; like $@ => qr[Too many arguments for Readonly::Readonly], 'Readonly Array reassign die'; ok eq_array(\@a2, [qw[another array value]]) => 'Readonly Array reassign no effect'; # Reassign hash eval q{Readonly %h2 => {another => "bad", hash => "!"}}; like $@ => qr[Attempt to delete], 'Readonly Hash reassign die'; ok eq_hash(\%h2, {another => 'hash', of => 'things'}) => 'Readonly Hash reassign no effect'; }; # Reassign real constants eval q{Readonly::Scalar "hello" => "goodbye"}; ok defined $@, 'Reassign real string'; # done_testing; scalar.t000644000000000000 204312743143001 17256 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/general#!perl -I../../lib # Readonly scalar tests use strict; use Test::More; use ReadonlyX; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n"; } use vars qw/$s1 $s2/; my ($ms1, $ms2); # creation (4 tests) eval {Readonly::Scalar $s1 => 13}; is $@ => '', 'Create a global scalar'; eval {Readonly::Scalar $ms1 => 31}; is $@ => '', 'Create a lexical scalar'; eval {Readonly::Scalar $s2 => undef}; is $@ => '', 'Create an undef global scalar'; #eval 'Readonly::Scalar $ms2'; # must be eval string because it's a compile-time error #like $@ => qr/^Not enough arguments for Readonly::Scalar/, 'Try w/o args'; # fetching (4 tests) is $s1 => 13, 'Fetch global'; is $ms1 => 31, 'Fetch lexical'; ok !defined $s2, 'Fetch undef global'; ok !defined $ms2, 'Fetch undef lexical'; # storing (2 tests) eval {$s1 = 7}; is $@ => expected(__LINE__-1), 'Error setting global'; is $s1 => 13, 'Readonly global value unchanged'; # done_testing; 001_predefined_var.t000644000000000000 263012743143001 20202 0ustar00unknownunknown000000000000ReadonlyX-1.04/t/x#!perl -I../../lib # Verify we don't clobber content of pre-defined variables w/o new value use strict; use Test::More; use ReadonlyX; use Test::Fatal; # { my $scalar = 'test'; Readonly::Scalar $scalar; is $scalar, 'test', 'predefined scalar is not clobbered'; } { my $scalar = 'test'; Readonly::Scalar $scalar => 'new value'; is $scalar, 'new value', 'clobber predefined scalar if given a new value'; } { my @array = (qw[test reset]); Readonly::Array @array; is_deeply \@array, [qw'test reset'], 'predefined list is not clobbered'; } { my @array = (qw[test reset]); Readonly::Array @array => [qw'new list']; is_deeply \@array, [qw'new list'], 'clobber predefined list if given a new value'; } { my @array = (qw[test reset]); Readonly::Array @array => ('new', 'list'); is_deeply \@array, [qw'new list'], 'clobber predefined list if given a new value (part two!)'; } { my %hash = %INC; Readonly::Hash %hash; is_deeply \%hash, \%INC, 'predefined hash is not clobbered'; } { my %hash = %INC; Readonly::Hash %hash => {fun => 'new junk'}; is_deeply \%hash, {fun => 'new junk'}, 'clobber predefined hash if given a new value'; } { my %hash = %INC; Readonly::Hash %hash => (fun => 'new junk'); is_deeply \%hash, {fun => 'new junk'}, 'clobber predefined hash if given a new value (part two!)'; } # done_testing; META.yml000644000000000000 222612743143001 15220 0ustar00unknownunknown000000000000ReadonlyX-1.04--- abstract: 'Faster facility for creating read-only scalars, arrays, hashes' author: - 'Sanko Robinson - http://sankorobinson.com/' build_requires: Test::Fatal: '0' Test::More: '0' configure_requires: Module::Build::Tiny: '0.035' dynamic_config: 0 generated_by: 'Minilla/v3.0.2, CPAN::Meta::Converter version 2.150005' license: artistic_2 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: ReadonlyX no_index: directory: - t - xt - inc - share - eg - examples - author - builder provides: ReadonlyX: file: lib/ReadonlyX.pm version: '1.04' requires: perl: '5.008' resources: bugtracker: https://github.com/sanko/readonly/issues homepage: https://github.com/sanko/readonly repository: git://github.com/sanko/readonly.git version: '1.04' x_contributors: - 'David Steinbrunner ' - 'Peter Valdemar Mørch ' - 'vti ' - 'Ronald Schmidt ' - 'Michael Ivanchenko ' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' MANIFEST000644000000000000 57212743143001 15062 0ustar00unknownunknown000000000000ReadonlyX-1.04Build.PL Changes LICENSE META.json README.md cpanfile eg/benchmark.pl lib/ReadonlyX.pm minil.toml t/bugs/007_implicit_undef.t t/bugs/025_partial_clone.t t/general/array.t t/general/clone.t t/general/deepa.t t/general/deeph.t t/general/deeps.t t/general/docs.t t/general/export.t t/general/hash.t t/general/reassign.t t/general/scalar.t t/x/001_predefined_var.t META.yml MANIFEST