Readonly-2.00/0000775000000000000000000000000012355037257011721 5ustar rootrootReadonly-2.00/t/0000775000000000000000000000000012355037257012164 5ustar rootrootReadonly-2.00/t/general/0000775000000000000000000000000012355037257013601 5ustar rootrootReadonly-2.00/t/general/export.t0000644000000000000000000000026612354243333015302 0ustar rootroot#!perl -I../../lib # Readonly hash tests use strict; use Test::More tests => 1; # Find the module (1 test) BEGIN { use_ok('Readonly', qw/Scalar Scalar1 Array Array1 Hash Hash1/); } Readonly-2.00/t/general/readonly.t0000644000000000000000000000427612354243333015603 0ustar rootroot#!perl -I../../lib # Test the Readonly function use strict; use Test::More tests => 19; # Find the module (1 test) BEGIN {use_ok('Readonly'); } my $expected = qr/Modification of a read-only value attempted at \(eval \d+\),? line 1/; SKIP: { skip 'Readonly \\ syntax is for perls earlier than 5.8', 9 if $] >= 5.008; eval q{Readonly \my $ros => 45}; is $@ => '', 'Create scalar'; eval q{Readonly \my $ros2 => 45; $ros2 = 45}; like $@ => $expected, 'Modify scalar'; eval q{Readonly \my @roa => (1, 2, 3, 4)}; is $@ => '', 'Create array'; eval q{Readonly \my @roa2 => (1, 2, 3, 4); $roa2[2] = 3}; like $@ => $expected, 'Modify array'; eval q{Readonly \my %roh => (key1 => "value", key2 => "value2")}; is $@ => '', 'Create hash (list)'; eval q{Readonly \my %roh => (key1 => "value", "key2")}; like $@ => qr/odd number of values/, 'Odd number of values'; eval q{Readonly \my %roh2 => (key1 => "value", key2 => "value2"); $roh2{key1}="value"}; like $@ => $expected, 'Modify hash'; eval q{Readonly \my %roh => {key1 => "value", key2 => "value2"}}; is $@ => '', 'Create hash (hashref)'; eval q{Readonly \my %roh2 => {key1 => "value", key2 => "value2"}; $roh2{key1}="value"}; like $@ => $expected, 'Modify hash'; }; SKIP: { skip 'Readonly $@% syntax is for perl 5.8 or later', 9 unless $] >= 5.008; eval q{Readonly my $ros => 45}; is $@ => '', 'Create scalar'; eval q{Readonly my $ros2 => 45; $ros2 = 45}; like $@ => $expected, 'Modify scalar'; eval q{Readonly my @roa => (1, 2, 3, 4)}; is $@ => '', 'Create array'; eval q{Readonly my @roa2 => (1, 2, 3, 4); $roa2[2] = 3}; like $@ => $expected, 'Modify array'; eval q{Readonly my %roh => (key1 => "value", key2 => "value2")}; is $@ => '', 'Create hash (list)'; eval q{Readonly my %roh => (key1 => "value", "key2")}; like $@ => qr/odd number of values/, 'Odd number of values'; eval q{Readonly my %roh2 => (key1 => "value", key2 => "value2"); $roh2{key1}="value"}; like $@ => $expected, 'Modify hash'; eval q{Readonly my %roh => {key1 => "value", key2 => "value2"}}; is $@ => '', 'Create hash (hashref)'; eval q{Readonly my %roh2 => {key1 => "value", key2 => "value2"}; $roh2{key1}="value"}; like $@ => $expected, 'Modify hash'; }; Readonly-2.00/t/general/deepa.t0000644000000000000000000000227312354243333015037 0ustar rootroot#!perl -I../../lib # Test Array vs Array1 functionality use strict; use Test::More tests => 13; # Find the module (1 test) BEGIN {use_ok('Readonly'); } 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 $m1 = 17; # Create (2 tests) eval {Readonly::Array @a1 => (\$m1, {x => 5, z => [1, 2, 3]})}; is $@ => '', 'Create a deep reference array'; eval {Readonly::Array1 @a2 => (\$m1, {x => 5, z => [1, 2, 3]})}; is $@ => '', 'Create a shallow reference array'; # Modify (10 tests) eval {$a1[0] = 7;}; is $@ => expected(__LINE__-1), 'Modify a1'; eval {$a2[0] = 7;}; is $@ => expected(__LINE__-1), 'Modify a2'; eval {${$a1[0]} = "the";}; is $@ => expected(__LINE__-1), 'Deep-modify a1'; is $m1 => 17, 'a1 unchanged'; eval {${$a2[0]} = "the";}; is $@ => '', 'Deep-modify a2'; is $m1 => 'the', 'a2 modification successful'; eval {$a1[1]{z}[1] = 42;}; is $@ => expected(__LINE__-1), 'Deep-deep modify a1'; is $a1[1]{z}[1] => 2, 'a1 unchanged'; eval {$a2[1]{z}[2] = 42;}; is $@ => '', 'Deep-deep modify a2'; is $a2[1]{z}[2], 42, 'a2 mod successful'; Readonly-2.00/t/general/scalar.t0000644000000000000000000000243512354243333015226 0ustar rootroot#!perl -I../../lib # Readonly scalar tests use strict; use Test::More tests => 12; # Find the module (1 test) BEGIN {use_ok('Readonly'); } 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'; # untie (1 test) SKIP:{ skip "Can't catch 'untie' until perl 5.6", 1 if $] < 5.006; skip "Scalars not tied: XS in use", 1 if $Readonly::XSokay; eval {untie $ms1}; is $@ => expected(__LINE__-1), 'Untie'; } Readonly-2.00/t/general/docs.t0000644000000000000000000000442612354243333014713 0ustar rootroot#!perl -I../../lib # Examples from the docs -- make sure they work! use strict; use Test::More tests => 22; # Find the module (1 test) BEGIN { use_ok('Readonly'); } 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 values/, 'Odd number of values'; # Shallow vs deep (8 tests) use vars qw/@shal @deep/; eval { Readonly::Array1 @shal => (1, 2, {perl => "Rules", java => "Bites"}, 4, 5); }; eval { Readonly::Array @deep => (1, 2, {perl => "Rules", java => "Bites"}, 4, 5); }; eval { $shal[1] = 7 }; is $@ => expected(__LINE__- 1), 'deep test 1'; is $shal[1] => 2, 'deep test 1 confirm'; eval { $shal[2]{APL} = "Weird" }; is $@ => '', 'deep test 2'; is $shal[2]{APL} => "Weird", 'deep test 2 confirm'; eval { $deep[1] = 7 }; is $@ => expected(__LINE__- 1), 'deep test 3'; is $deep[1] => 2, 'deep test 3 confirm'; eval { $deep[2]{APL} = "Weird" }; is $@ => expected(__LINE__- 1), 'deep test 4'; ok !exists($deep[2]{APL}), 'deep test 4 confirm'; Readonly-2.00/t/general/deeps.t0000644000000000000000000000333512354243333015061 0ustar rootroot#!perl -I../../lib # Test Scalar vs Scalar1 functionality use strict; use Test::More tests => 21; # Find the module (1 test) BEGIN {use_ok('Readonly'); } 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 $s3 $s4/; my $m1 = 17; my $m2 = \$m1; # Create (4 tests) eval {Readonly::Scalar1 $s1 => ["this", "is", "a", "test", {x => 5}]}; is $@ => '', 'Create a shallow reference scalar'; eval {Readonly::Scalar $s2 => ["this", "is", "a", "test", {x => 5}]}; is $@ => '', 'Create a deep reference scalar'; eval {Readonly::Scalar1 $s3 => $m2}; is $@ => '', 'Create a shallow scalar ref'; eval {Readonly::Scalar $s4 => $m2}; is $@ => '', 'Create a deep scalar ref'; # Modify (16 tests) eval {$s1 = 7}; is $@ => expected(__LINE__-1), 'Modify s1'; eval {$s2 = 7}; is $@ => expected(__LINE__-1), 'Modify s2'; eval {$s3 = 7}; is $@ => expected(__LINE__-1), 'Modify s3'; eval {$s4 = 7}; is $@ => expected(__LINE__-1), 'Modify s4'; eval {$s1->[2] = "the"}; is $@ => '', 'Deep-modify s1'; is $s1->[2] => 'the', 's1 modification successful'; eval {$s2->[2] = "the"}; is $@ => expected(__LINE__-1), 'Deep-modify s2'; is $s2->[2] => 'a', 's2 modification supposed to fail'; eval {$s1->[4]{z} = 42}; is $@ => '', 'Deep-deep modify s1'; is $s1->[4]{z} => 42, 's1 mod successful'; eval {$s2->[4]{z} = 42}; is $@ => expected(__LINE__-1), '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'; eval {$$s3 = "bah"}; is $@ => '', 'deep s3 mod'; is $m1 => 'bah', 'deep s3 mod'; Readonly-2.00/t/general/tie.t0000644000000000000000000000113712354243333014540 0ustar rootroot#!perl -I../../lib # Test the Readonly function use strict; use Test::More tests => 4; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Invalid tie at " . __FILE__ . " line $line\n"; } # Find the module (1 test) BEGIN { use_ok('Readonly'); } eval { tie my $s, 'Readonly::Scalar', 1 }; is $@ => expected(__LINE__- 1), "Direct scalar tie"; eval { tie my @a, 'Readonly::Array', 2, 3, 4 }; is $@ => expected(__LINE__- 1), "Direct array tie"; eval { tie my %h, 'Readonly::Hash', five => 5, six => 6 }; is $@ => expected(__LINE__- 1), "Direct hash tie"; Readonly-2.00/t/general/hash.t0000644000000000000000000000322212354243333014677 0ustar rootroot#!perl -I../../lib # Readonly hash tests use strict; use Test::More tests => 20; # Find the module (1 test) BEGIN {use_ok('Readonly'); } 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'; eval {Readonly::Hash %mh1 => (one=>1, two=>2, three=>3, 4)}; like $@ => qr/odd number of values/, "Odd number of values"; 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'; ok !defined $h1{'q'}, 'Nonexistent element undefined'; 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}}; is $@ => expected(__LINE__-1), 'Delete'; # clear (1 test) eval {%h1 = ()}; is $@ => expected(__LINE__-1), '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'; # untie (1 test) SKIP: { skip "Can't catch untie until Perl 5.6", 1 if $] < 5.006; eval {untie %h1}; is $@ => expected(__LINE__-1), 'Untie'; } Readonly-2.00/t/general/reassign.t0000644000000000000000000000603312354243333015572 0ustar rootroot#!perl -I../../lib # Readonly reassignment-prevention tests use strict; use Test::More tests => 22; # Find the module (1 test) BEGIN {use_ok('Readonly'); } 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/^Attempt to reassign/; # 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 $@ => $err, '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; 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 constants eval q{Readonly::Scalar "hello" => "goodbye"}; ok defined $@, 'Reassign real string'; eval q{Readonly::Scalar1 6 => 13}; ok defined $@, 'Reassign real number'; Readonly-2.00/t/general/deeph.t0000644000000000000000000000235112354243333015043 0ustar rootroot#!perl -I../../lib # Test Hash vs Hash1 functionality use strict; use Test::More tests => 13; # Find the module (1 test) BEGIN {use_ok('Readonly'); } 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 %h2/; 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'; eval {Readonly::Hash1 %h2 => (key1 => \$m1, key2 => {x => 5, z => [1, 2, 3]})}; is $@ => '', 'Create a shallow reference array'; # Modify (10 tests) eval {$h1{key1} = 7}; is $@ => expected(__LINE__-1), 'Modify h1'; eval {$h2{key1} = 7}; is $@ => expected(__LINE__-1), 'Modify h2'; eval {${$h1{key1}} = "the"}; is $@ => expected(__LINE__-1), 'Deep-modify h1'; is $m1 => 17, 'h1 unchanged'; eval {${$h2{key1}} = "the"}; is $@ => '', 'Deep-modify h2'; is $m1 => 'the', 'h2 modification successful'; eval {$h1{key2}{z}[1] = 42}; is $@ => expected(__LINE__-1), 'Deep-deep modify h1'; is $h1{key2}{z}[1] => 2, 'h1 unchanged'; eval {$h2{key2}{z}[2] = 42}; is $@ => '', 'Deep-deep modify h2'; is $h2{key2}{z}[2], 42, 'h2 mod successful'; Readonly-2.00/t/general/array.t0000644000000000000000000000377112354243333015103 0ustar rootroot#!perl -I../../lib # Readonly array tests use strict; use Test::More tests => 23; # Find the module (1 test) BEGIN {use_ok('Readonly'); } 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) 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'; # 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'; # splice (1 test) eval {splice @a2, 0, 1;}; is $@ => expected(__LINE__-1), 'Splice'; # untie (1 test) SKIP: { skip "Can't catch untie until Perl 5.6", 1 if $] <= 5.006; eval {untie @a2;}; is $@ => expected(__LINE__-1), 'Untie'; } Readonly-2.00/t/bugs/0000775000000000000000000000000012355037257013124 5ustar rootrootReadonly-2.00/t/bugs/007_implicit_undef.t0000644000000000000000000000110312354243333016654 0ustar rootroot#!perl -I../../lib # Verify the Readonly function accepts implicit undef values use strict; use Test::More tests => 3; sub expected { my $line = shift; $@ =~ s/\.$//; # difference between croak and die return "Invalid tie at " . __FILE__ . " line $line\n"; } # Find the module (1 test) BEGIN { use_ok('Readonly'); } eval 'Readonly my $simple;'; is $@ => '', 'Simple API allows for implicit undef values'; eval q'Readonly::Scalar my $scalar;'; like $@ => qr[Not enough arguments for Readonly::Scalar], 'Readonly::Scalar does not allow implicit undef values'; Readonly-2.00/Changes0000644000000000000000000001104212354243333013201 0ustar rootrootRevision history for Perl extension Readonly. 2.00 2014-06-30T11:15:05Z - Deprecation of Readonly::XS as a requirement for fast, readonly scalars is complete. Report any lingering issues on the tracker ASAP. 1.61 2014-06-28T11:22:13Z - Normal constants (strings, numbers) do not appear to be read only to Internals::SvREADONLY($) but perl itself doesn't miss a beat when you attempt to assign a value to them. Fixing test regression in t/general/reassign.t 1.60 2014-06-27T15:59:27Z - Fix array and hash tie() while in XS mode (exposed by Params::Validate tests) - Fix implicit undef value regression resolves #8 - Minor documentation fixes (spell check, etc.) - Patch from Gregor Herrmann resolves #7 v1.500.0 2014-06-25T19:56:18Z - PLEASE NOTE: Readonly::XS is no longer needed! - Again, Readonly::XS is no longer needed. - Merged typo fix from David Steinbrunner RT#86350/#2 - Merged patch (w/ tests, yay!) from Daniel P. Risse RT#37864 - Upstream magic related bugs were reported to p5p and fixed in perl itself so we can resolve the following local issues: RT#70167, RT#57382, RT#29487, RT#36653, RT#24216. - Reported RT#120122 (tie + smartmatch bug) upstream to p5p. Will eventually resolve local [RT#59256]. - Note: Resolved RT#16167 (benchmark.pl being installed) in 1.04. - Use readonly support exposed in Internals on perl >=5.8.x - Have I mentioned you don't need to install Readonly::XS anymore? - Checking $Readonly::XSokay is no longer suggested. ...never should have been 1.04 2013-11-26T01:20:38Z - Module now maintained by Sanko Robinson. Please see TODO for a possible set of changes to this module that may effect code written for old, pre- perl 5.14.0 platforms!!! 1.03 2004 April 20 - Changed the prototype for Readonly, to make the usage cleaner. Unfortunately, this breaks backwards-compatability for this function. Users of this function who have Perl 5.8 or later will have to change their source code. Also, users of this function who upgrade to perl 5.8+ will have to change their usage. Having discussed this feature change with a number of people, I felt that breaking compatability was worth the gain in simplicity of usage. (Thanks to Damian Conway for the suggestion). - Removed "use warnings" so the module will work in perl 5.005. 1.02 2003 May 13 - If Readonly::XS is installed, Readonly will use it for making scalars read-only. - Callers are now forbidden to tie variables directly. This prevents sneaky callers from reassigning a variable via tie. - Error messages have been changed to be more like Perl's own "Modification of a read-only value attempted at..." - Catch and return an error if user tries to pass a constant to Readonly::Scalar (eg Readonly::Scalar 'hello', 'goodbye') - Include a simple benchmark script. - Add a few more test cases. You can never have too many. - Add a simple benchmark program. 1.01 2003 February 14 - Add some checking to prevent reassignment of Readonly variables. - Changed my email address in the docs. 1.00 2003 January 7 - No code changes. No bugs or suggestions have been reported for six months, so the version number is changing to 1.00. 0.07 2002 June 25 - Clean up the code somewhat; remove redundancies; delay loading Carp.pm until it's needed. - Fixed the list of EXPORT_OK symbols. 0.06 2002 June 16 - Add Readonly function, to provide a unified (and shorter) way to create readonly variables. (Thanks to Slaven Rezic for the idea). - Scalar, Array, and Hash now mark entire data structures as Readonly. Added Scalar1, Array1, and Hash1 for shallow Readonly protection. (Thanks to Ernest Lergon for the idea). - Switch to Test::More and Test::Harness. 134 tests now! 0.05 2002 March 15 - Change name from Constant.pm to Readonly.pm, due to file naming conflict under Windows. - Changed docs to match. - Allow Readonly::Hash to accept a hash reference parameter. - Works better with older versions of Perl. - Add many, many test cases to test.pl. 0.04 2002 March 7 - Add top-level Scalar, Array, and Hash functions, so callers don't have to tie the variables themselves. 0.03 2001 September 9 - documentation changes only. 0.02 2001 September 9 - documentation changes only. 0.01 2001 August 30 - Constant.pm, original version. Readonly-2.00/Build.PL0000644000000000000000000000350212354243333013204 0ustar rootroot# ========================================================================= # THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. # DO NOT EDIT DIRECTLY. # ========================================================================= use 5.008_001; use strict; use warnings; use utf8; use Module::Build; use File::Basename; use File::Spec; use CPAN::Meta; use CPAN::Meta::Prereqs; my %args = ( license => 'perl', dynamic_config => 0, configure_requires => { 'Module::Build' => 0.38, }, name => 'Readonly', module_name => 'Readonly', allow_pureperl => 0, script_files => [glob('script/*'), glob('bin/*')], c_source => [qw()], PL_files => {}, test_files => ((-d '.git' || $ENV{RELEASE_TESTING}) && -d 'xt') ? 't/ xt/' : 't/', recursive_test_files => 1, ); if (-d 'share') { $args{share_dir} = 'share'; } my $builder = Module::Build->subclass( class => 'MyBuilder', code => q{ sub ACTION_distmeta { die "Do not run distmeta. Install Minilla and `minil install` instead.\n"; } sub ACTION_installdeps { die "Do not run installdeps. Run `cpanm --installdeps .` instead.\n"; } } )->new(%args); $builder->create_build_script(); my $mbmeta = CPAN::Meta->load_file('MYMETA.json'); my $meta = CPAN::Meta->load_file('META.json'); my $prereqs_hash = CPAN::Meta::Prereqs->new( $meta->prereqs )->with_merged_prereqs( CPAN::Meta::Prereqs->new($mbmeta->prereqs) )->as_string_hash; my $mymeta = CPAN::Meta->new( { %{$meta->as_struct}, prereqs => $prereqs_hash } ); print "Merging cpanfile prereqs to MYMETA.yml\n"; $mymeta->save('MYMETA.yml', { version => 1.4 }); print "Merging cpanfile prereqs to MYMETA.json\n"; $mymeta->save('MYMETA.json', { version => 2 }); Readonly-2.00/eg/0000775000000000000000000000000012355037257012314 5ustar rootrootReadonly-2.00/eg/benchmark.pl0000644000000000000000000000327112354243333014575 0ustar rootroot#!/usr/bin/perl # Very simple benchmark script to show how slow Readonly.pm is, # and how Readonly::XS solves the problem. use strict; use lib '../lib'; use Readonly; use Benchmark; use vars qw/$feedme/; # # use constant # use constant CONST_LINCOLN => 'Fourscore and seven years ago...'; sub const { $feedme = CONST_LINCOLN; } # # literal constant # sub literal { $feedme = 'Fourscore and seven years ago...'; } # # typeglob constant # use vars qw/$glob_lincoln/; *glob_lincoln = \'Fourscore and seven years ago...'; sub tglob { $feedme = $glob_lincoln; } # # Normal perl read/write scalar # use vars qw/$norm_lincoln/; $norm_lincoln = 'Fourscore and seven years ago...'; sub normal { $feedme = $norm_lincoln; } # # Readonly.pm with verbose API # use vars qw/$ro_lincoln/; Readonly::Scalar $ro_lincoln => 'Fourscore and seven years ago...'; sub ro { $feedme = $ro_lincoln; } # # Readonly.pm with simple API # use vars qw/$ro_simple_lincoln/; Readonly $ro_simple_lincoln => 'Fourscore and seven years ago...'; sub ro_simple { $feedme = $ro_simple_lincoln; } # # Readonly.pm w/o Readonly::XS # use vars qw/$rotie_lincoln/; { local $Readonly::XSokay = 0; # disable XS Readonly::Scalar $rotie_lincoln => 'Fourscore and seven years ago...'; } sub rotie { $feedme = $rotie_lincoln; } my $code = {const => \&const, literal => \&literal, tglob => \&tglob, normal => \&normal, ro => \&ro, ro_simple => \&ro_simple, rotie => \&rotie, }; unless ($Readonly::XSokay) { print "Readonly::XS module not found; skipping that test.\n"; delete $code->{roxs}; } timethese(2_000_000, $code); Readonly-2.00/LICENSE0000644000000000000000000004374212354243333012727 0ustar rootrootThis 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 EndReadonly-2.00/cpanfile0000644000000000000000000000012412354243333013411 0ustar rootrootrecommends 'perl', '5.20.0'; requires 'perl', '5.6.0'; test_requires 'Test::More'; Readonly-2.00/META.json0000644000000000000000000000410312354243333013327 0ustar rootroot{ "abstract" : "Facility for creating read-only scalars, arrays, hashes", "author" : [ "Sanko Robinson - http://sankorobinson.com/" ], "dynamic_config" : 0, "generated_by" : "Minilla/v1.1.0", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Readonly", "no_index" : { "directory" : [ "t", "xt", "inc", "share", "eg", "examples", "author", "builder" ] }, "prereqs" : { "configure" : { "requires" : { "CPAN::Meta" : "0", "CPAN::Meta::Prereqs" : "0", "Module::Build" : "0.38" } }, "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" : { "recommends" : { "perl" : "v5.20.0" }, "requires" : { "perl" : "v5.6.0" } }, "test" : { "requires" : { "Test::More" : "0" } } }, "provides" : { "Readonly" : { "file" : "lib/Readonly.pm", "version" : "2.00" }, "Readonly::Array" : { "file" : "lib/Readonly.pm" }, "Readonly::Hash" : { "file" : "lib/Readonly.pm" }, "Readonly::Scalar" : { "file" : "lib/Readonly.pm" } }, "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" : "2.00", "x_contributors" : [ "David Steinbrunner " ] } Readonly-2.00/MANIFEST0000644000000000000000000000051512354243333013042 0ustar rootrootChanges LICENSE README.md cpanfile eg/benchmark.pl lib/Readonly.pm t/bugs/007_implicit_undef.t t/general/array.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/readonly.t t/general/reassign.t t/general/scalar.t t/general/tie.t Build.PL META.json META.yml MANIFESTReadonly-2.00/README.md0000644000000000000000000003475212354243333013202 0ustar rootroot# NAME Readonly - Facility for creating read-only scalars, arrays, hashes # Synopsis use Readonly; # Deep Read-only scalar Readonly::Scalar $sca => $initial_value; Readonly::Scalar my $sca => $initial_value; # Deep Read-only array Readonly::Array @arr => @values; Readonly::Array my @arr => @values; # Deep Read-only hash Readonly::Hash %has => (key => value, key => value, ...); Readonly::Hash my %has => (key => value, key => value, ...); # or: Readonly::Hash %has => {key => value, key => value, ...}; # You can use the read-only variables like any regular variables: print $sca; $something = $sca + $arr[2]; next if $has{$some_key}; # But if you try to modify a value, your program will die: $sca = 7; push @arr, 'seven'; delete $has{key}; # The error message is "Modification of a read-only value attempted" # Alternate form (Perl 5.8 and later) Readonly $sca => $initial_value; Readonly my $sca => $initial_value; Readonly @arr => @values; Readonly my @arr => @values; Readonly %has => (key => value, key => value, ...); Readonly my %has => (key => value, key => value, ...); Readonly my $sca; # Implicit undef, readonly value # Alternate form (for Perls earlier than v5.8) Readonly \$sca => $initial_value; Readonly \my $sca => $initial_value; Readonly \@arr => @values; Readonly \my @arr => @values; Readonly \%has => (key => value, key => value, ...); Readonly \my %has => (key => value, key => value, ...); # Description This is a facility for creating non-modifiable variables. This is useful for configuration files, headers, etc. It can also be useful as a development and debugging tool for catching updates to variables that should not be changed. # Variable Depth Readonly has the ability to create both deep and shallow readonly variables. If any of the values you pass to `Scalar`, `Array`, `Hash`, or the standard `Readonly` are references, then those functions recurse over the data structures, marking everything as Readonly. The entire structure is nonmodifiable. This is normally what you want. If you want only the top level to be Readonly, use the alternate (and poorly named) `Scalar1`, `Array1`, and `Hash1` functions. # # The Past The following sections are updated versions of the previous authors documentation. ## Comparison with "use constant" Perl provides a facility for creating constant values, via the [constant](https://metacpan.org/pod/constant) pragma. There are several problems with this pragma. - The constants created have no leading sigils. - These constants cannot be interpolated into strings. - Syntax can get dicey sometimes. For example: use constant CARRAY => (2, 3, 5, 7, 11, 13); $a_prime = CARRAY[2]; # wrong! $a_prime = (CARRAY)[2]; # right -- MUST use parentheses - You have to be very careful in places where barewords are allowed. For example: use constant SOME_KEY => 'key'; %hash = (key => 'value', other_key => 'other_value'); $some_value = $hash{SOME_KEY}; # wrong! $some_value = $hash{+SOME_KEY}; # right (who thinks to use a unary plus when using a hash to scalarize the key?) - `use constant` works for scalars and arrays, not hashes. - These constants are global to the package in which they're declared; cannot be lexically scoped. - Works only at compile time. - Can be overridden: use constant PI => 3.14159; ... use constant PI => 2.71828; (this does generate a warning, however, if you have warnings enabled). - It is very difficult to make and use deep structures (complex data structures) with `use constant`. # Comparison with typeglob constants Another popular way to create read-only scalars is to modify the symbol table entry for the variable by using a typeglob: *a = \'value'; This works fine, but it only works for global variables ("my" variables have no symbol table entry). Also, the following similar constructs do **not** work: *a = [1, 2, 3]; # Does NOT create a read-only array *a = { a => 'A'}; # Does NOT create a read-only hash ## Pros Readonly.pm, on the other hand, will work with global variables and with lexical ("my") variables. It will create scalars, arrays, or hashes, all of which look and work like normal, read-write Perl variables. You can use them in scalar context, in list context; you can take references to them, pass them to functions, anything. Readonly.pm also works well with complex data structures, allowing you to tag the whole structure as nonmodifiable, or just the top level. Also, Readonly variables may not be reassigned. The following code will die: Readonly::Scalar $pi => 3.14159; ... Readonly::Scalar $pi => 2.71828; ## Cons Readonly.pm used to impose a performance penalty. It was pretty slow. How slow? Run the `eg/benchmark.pl` script that comes with Readonly. On my test system, "use constant" (const), typeglob constants (tglob), regular read/write Perl variables (normal/literal), and the new Readonly (ro/ro\_simple) are all about the same speed, the old, tie based Readonly.pm constants were about 1/22 the speed. However, there is relief. There is a companion module available, Readonly::XS. You won't need this if you're using Perl 5.8.x or higher. I repeat, you do not need Readonly::XS if your environment has perl 5.8.x or higher. Please see section entitled [Internals](#internals) for more. # Functions - Readonly::Scalar $var => $value; Creates a nonmodifiable scalar, `$var`, and assigns a value of `$value` to it. Thereafter, its value may not be changed. Any attempt to modify the value will cause your program to die. A value _must_ be supplied. If you want the variable to have `undef` as its value, you must specify `undef`. If `$value` is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the `$value` marked as Readonly, use `Scalar1`. If $var is already a Readonly variable, the program will die with an error about reassigning Readonly variables. - Readonly::Array @arr => (value, value, ...); Creates a nonmodifiable array, `@arr`, and assigns the specified list of values to it. Thereafter, none of its values may be changed; the array may not be lengthened or shortened or spliced. Any attempt to do so will cause your program to die. If any of the values passed is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the hash `%@arr` itself marked as Readonly, use `Array1`. If `@arr` is already a Readonly variable, the program will die with an error about reassigning Readonly variables. - Readonly::Hash %h => (key => value, key => value, ...); - Readonly::Hash %h => {key => value, key => value, ...}; Creates a nonmodifiable hash, `%h`, and assigns the specified keys and values to it. Thereafter, its keys or values may not be changed. Any attempt to do so will cause your program to die. A list of keys and values may be specified (with parentheses in the synopsis above), or a hash reference may be specified (curly braces in the synopsis above). If a list is specified, it must have an even number of elements, or the function will die. If any of the values is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the hash `%h` itself marked as Readonly, use `Hash1`. If `%h` is already a Readonly variable, the program will die with an error about reassigning Readonly variables. - Readonly $var => $value; - Readonly @arr => (value, value, ...); - Readonly %h => (key => value, ...); - Readonly %h => {key => value, ...}; - Readonly $var; The `Readonly` function is an alternate to the `Scalar`, `Array`, and `Hash` functions. It has the advantage (if you consider it an advantage) of being one function. That may make your program look neater, if you're initializing a whole bunch of constants at once. You may or may not prefer this uniform style. It has the disadvantage of having a slightly different syntax for versions of Perl prior to 5.8. For earlier versions, you must supply a backslash, because it requires a reference as the first parameter. Readonly \$var => $value; Readonly \@arr => (value, value, ...); Readonly \%h => (key => value, ...); Readonly \%h => {key => value, ...}; You may or may not consider this ugly. Note that you can create implicit undefined variables with this function like so `Readonly my $var;` while a verbose undefined value must be passed to the standard `Scalar`, `Array`, and `Hash` functions. - Readonly::Scalar1 $var => $value; - Readonly::Array1 @arr => (value, value, ...); - Readonly::Hash1 %h => (key => value, key => value, ...); - Readonly::Hash1 %h => {key => value, key => value, ...}; These alternate functions create shallow Readonly variables, instead of deep ones. For example: Readonly::Array1 @shal => (1, 2, {perl=>'Rules', java=>'Bites'}, 4, 5); Readonly::Array @deep => (1, 2, {perl=>'Rules', java=>'Bites'}, 4, 5); $shal[1] = 7; # error $shal[2]{APL}='Weird'; # Allowed! since the hash isn't Readonly $deep[1] = 7; # error $deep[2]{APL}='Weird'; # error, since the hash is Readonly # Examples These are a few very simple examples: ## Scalars A plain old read-only value Readonly::Scalar $a => "A string value"; The value need not be a compile-time constant: Readonly::Scalar $a => $computed_value; ## Arrays/Lists A read-only array: Readonly::Array @a => (1, 2, 3, 4); The parentheses are optional: Readonly::Array @a => 1, 2, 3, 4; You can use Perl's built-in array quoting syntax: Readonly::Array @a => qw/1 2 3 4/; You can initialize a read-only array from a variable one: Readonly::Array @a => @computed_values; A read-only array can be empty, too: Readonly::Array @a => (); Readonly::Array @a; # equivalent ## Hashes Typical usage: Readonly::Hash %a => (key1 => 'value1', key2 => 'value2'); A read-only hash can be initialized from a variable one: Readonly::Hash %a => %computed_values; A read-only hash can be empty: Readonly::Hash %a => (); Readonly::Hash %a; # equivalent If you pass an odd number of values, the program will die: Readonly::Hash %a => (key1 => 'value1', "value2"); # This dies with "May not store an odd number of values in a hash" # Exports Historically, this module exports the `Readonly` symbol into the calling program's namespace by default. The following symbols are also available for import into your program, if you like: `Scalar`, `Scalar1`, `Array`, `Array1`, `Hash`, and `Hash1`. # Internals Some people simply do not understand the relationship between this module and Readonly::XS so I'm adding this section. Odds are, they still won't understand but I like to write so... In the past, Readonly's "magic" was performed by `tie()`-ing variables to the `Readonly::Scalar`, `Readonly::Array`, and `Readonly::Hash` packages (not to be confused with the functions of the same names) and acting on `WRITE`, `READ`, et. al. While this worked well, it was slow. Very slow. Like 20-30 times slower than accessing variables directly or using one of the other const-related modules that have cropped up since Readonly was released in 2003. To 'fix' this, Readonly::XS was written. If installed, Readonly::XS used the internal methods `SvREADONLY` and `SvREADONLY_on` to lock simple scalars. On the surface, everything was peachy but things weren't the same behind the scenes. In edge cases, code perfromed very differently if Readonly::XS was installed and because it wasn't a required dependency in most code, it made downstream bugs very hard to track. In the years since Readonly::XS was released, the then private internal methods have been exposed and can be used in pure perl. Similar modules were written to take advantage of this and a patch to Readonly was created. We no longer need to build and install another module to make Readonly useful on modern builds of perl. - You do not need to install Readonly::XS. - You should stop listing Readonly::XS as a dependency or expect it to be installed. - Stop testing the `$Readonly::XSokay` variable! # Requirements Please note that most users of Readonly no longer need to install the companion module Readonly::XS which is recommended but not required for perl 5.6.x and under. Please do not force it as a requirement in new code and do not use the package variable `$Readonly::XSokay` in code/tests. For more, see ["Internals" in the section on Readonly's new internals](https://metacpan.org/pod/the section on Readonly's new internals#Internals). 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. Please check the TODO file included with this distribution in case your bug is already known (...I probably won't file bug reports to myself). # Acknowledgements Thanks to Slaven Rezic for the idea of one common function (Readonly) for all three types of variables (13 April 2002). Thanks to Ernest Lergon for the idea (and initial code) for deeply-Readonly data structures (21 May 2002). Thanks to Damian Conway for the idea (and code) for making the Readonly function work a lot smoother under perl 5.8+. # Author Sanko Robinson - http://sankorobinson.com/ CPAN ID: SANKO Original author: Eric J. Roode, roode@cpan.org # License and Legal Copyright (C) 2013, 2014 by Sanko Robinson Copyright (c) 2001-2004 by Eric J. Roode. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Readonly-2.00/lib/0000775000000000000000000000000012355037257012467 5ustar rootrootReadonly-2.00/lib/Readonly.pm0000644000000000000000000005715212354243333014603 0ustar rootrootpackage Readonly; use 5.005; use strict; #use warnings; #no warnings 'uninitialized'; package Readonly; our $VERSION = '2.00'; $VERSION = eval $VERSION; # Autocroak (Thanks, MJD) # Only load Carp.pm if module is croaking. sub croak { require Carp; goto &Carp::croak; } # These functions may be overridden by Readonly::XS, if installed. use vars qw/$XSokay/; # Set to true in Readonly::XS, if available # For perl 5.8.x or higher # These functions are exposed in perl 5.8.x (Thanks, Leon!) # They may be overridden by Readonly::XS, if installed on old perl versions if ($] < 5.008) { # 'Classic' perl *is_sv_readonly = sub ($) {0}; *make_sv_readonly = sub ($) { die "make_sv_readonly called but not overridden" }; # See if we can use the XS stuff. $Readonly::XS::MAGIC_COOKIE = "Do NOT use or require Readonly::XS unless you're me."; eval 'use Readonly::XS'; } else { # Modern perl doesn't need Readonly::XS *is_sv_readonly = sub ($) { Internals::SvREADONLY($_[0]) }; *make_sv_readonly = sub ($) { Internals::SvREADONLY($_[0], 1) }; $XSokay = 1; # We're using the new built-ins so this is a white lie } # Common error messages, or portions thereof use vars qw/$MODIFY $REASSIGN $ODDHASH/; $MODIFY = 'Modification of a read-only value attempted'; $REASSIGN = 'Attempt to reassign a readonly'; $ODDHASH = 'May not store an odd number of values in a hash'; # ---------------- # Read-only scalars # ---------------- package Readonly::Scalar; sub TIESCALAR { my $whence = (caller 2)[3]; # Check if naughty user is trying to tie directly. Readonly::croak "Invalid tie" unless $whence && $whence =~ /^Readonly::(?:Scalar1?|Readonly)$/; my $class = shift; Readonly::croak "No value specified for readonly scalar" unless @_; Readonly::croak "Too many values specified for readonly scalar" unless @_ == 1; my $value = shift; return bless \$value, $class; } sub FETCH { my $self = shift; return $$self; } *STORE = *UNTIE = sub { Readonly::croak $Readonly::MODIFY}; # ---------------- # Read-only arrays # ---------------- package Readonly::Array; sub TIEARRAY { my $whence = (caller 1)[3]; # Check if naughty user is trying to tie directly. Readonly::croak "Invalid tie" unless $whence =~ /^Readonly::Array1?$/; my $class = shift; my @self = @_; return bless \@self, $class; } sub FETCH { my $self = shift; my $index = shift; return $self->[$index]; } sub FETCHSIZE { my $self = shift; return scalar @$self; } BEGIN { eval q{ sub EXISTS { my $self = shift; my $index = shift; return exists $self->[$index]; } } if $] >= 5.006; # couldn't do "exists" on arrays before then } *STORE = *STORESIZE = *EXTEND = *PUSH = *POP = *UNSHIFT = *SHIFT = *SPLICE = *CLEAR = *UNTIE = sub { Readonly::croak $Readonly::MODIFY}; # ---------------- # Read-only hashes # ---------------- package Readonly::Hash; sub TIEHASH { my $whence = (caller 1)[3]; # Check if naughty user is trying to tie directly. Readonly::croak "Invalid tie" unless $whence =~ /^Readonly::Hash1?$/; my $class = shift; # must have an even number of values Readonly::croak $Readonly::ODDHASH unless (@_ % 2 == 0); my %self = @_; return bless \%self, $class; } sub FETCH { my $self = shift; my $key = shift; return $self->{$key}; } sub EXISTS { my $self = shift; my $key = shift; return exists $self->{$key}; } sub FIRSTKEY { my $self = shift; my $dummy = keys %$self; return scalar each %$self; } sub NEXTKEY { my $self = shift; return scalar each %$self; } *STORE = *DELETE = *CLEAR = *UNTIE = sub { Readonly::croak $Readonly::MODIFY}; # ---------------------------------------------------------------- # Main package, containing convenience functions (so callers won't # have to explicitly tie the variables themselves). # ---------------------------------------------------------------- package Readonly; use Exporter; use vars qw/@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/; push @ISA, 'Exporter'; push @EXPORT, qw/Readonly/; push @EXPORT_OK, qw/Scalar Array Hash Scalar1 Array1 Hash1/; # Predeclare the following, so we can use them recursively sub Scalar ($$); sub Array (\@;@); sub Hash (\%;@); # Returns true if a string begins with "Readonly::" # Used to prevent reassignment of Readonly variables. sub _is_badtype { my $type = $_[0]; return lc $type if $type =~ s/^Readonly:://; return; } # Shallow Readonly scalar sub Scalar1 ($$) { croak "$REASSIGN scalar" if is_sv_readonly($_[0]); my $badtype = _is_badtype(ref tied $_[0]); croak "$REASSIGN $badtype" if $badtype; # xs method: flag scalar as readonly if ($XSokay) { $_[0] = $_[1]; make_sv_readonly($_[0]); return; } # pure-perl method: tied scalar my $tieobj = eval { tie $_[0], 'Readonly::Scalar', $_[1] }; if ($@) { croak "$REASSIGN scalar" if substr($@, 0, 43) eq $MODIFY; die $@; # some other error? } return $tieobj; } # Shallow Readonly array sub Array1 (\@;@) { my $badtype = _is_badtype(ref tied $_[0]); croak "$REASSIGN $badtype" if $badtype; my $aref = shift; return tie @$aref, 'Readonly::Array', @_; } # Shallow Readonly hash sub Hash1 (\%;@) { my $badtype = _is_badtype(ref tied $_[0]); croak "$REASSIGN $badtype" if $badtype; my $href = shift; # If only one value, and it's a hashref, expand it if (@_ == 1 && ref $_[0] eq 'HASH') { return tie %$href, 'Readonly::Hash', %{$_[0]}; } # otherwise, must have an even number of values croak $ODDHASH unless (@_ % 2 == 0); return tie %$href, 'Readonly::Hash', @_; } # Deep Readonly scalar sub Scalar ($$) { croak "$REASSIGN scalar" if is_sv_readonly($_[0]); my $badtype = _is_badtype(ref tied $_[0]); croak "$REASSIGN $badtype" if $badtype; my $value = $_[1]; # Recursively check passed element for references; if any, make them Readonly foreach ($value) { if (ref eq 'SCALAR') { Scalar my $v => $$_; $_ = \$v } elsif (ref eq 'ARRAY') { Array my @v => @$_; $_ = \@v } elsif (ref eq 'HASH') { Hash my %v => $_; $_ = \%v } } # xs method: flag scalar as readonly if ($XSokay) { $_[0] = $value; make_sv_readonly($_[0]); return; } # pure-perl method: tied scalar my $tieobj = eval { tie $_[0], 'Readonly::Scalar', $value }; if ($@) { croak "$REASSIGN scalar" if substr($@, 0, 43) eq $MODIFY; die $@; # some other error? } return $tieobj; } # Deep Readonly array sub Array (\@;@) { my $badtype = _is_badtype(ref tied @{$_[0]}); croak "$REASSIGN $badtype" if $badtype; my $aref = shift; my @values = @_; # Recursively check passed elements for references; if any, make them Readonly foreach (@values) { if (ref eq 'SCALAR') { Scalar my $v => $$_; $_ = \$v } elsif (ref eq 'ARRAY') { Array my @v => @$_; $_ = \@v } elsif (ref eq 'HASH') { Hash my %v => $_; $_ = \%v } } # Lastly, tie the passed reference return tie @$aref, 'Readonly::Array', @values; } # Deep Readonly hash sub Hash (\%;@) { my $badtype = _is_badtype(ref tied %{$_[0]}); croak "$REASSIGN $badtype" if $badtype; my $href = shift; my @values = @_; # If only one value, and it's a hashref, expand it if (@_ == 1 && ref $_[0] eq 'HASH') { @values = %{$_[0]}; } # otherwise, must have an even number of values croak $ODDHASH unless (@values % 2 == 0); # Recursively check passed elements for references; if any, make them Readonly foreach (@values) { if (ref eq 'SCALAR') { Scalar my $v => $$_; $_ = \$v } elsif (ref eq 'ARRAY') { Array my @v => @$_; $_ = \@v } elsif (ref eq 'HASH') { Hash my %v => $_; $_ = \%v } } return tie %$href, 'Readonly::Hash', @values; } # Common entry-point for all supported data types eval q{sub Readonly} . ($] < 5.008 ? '' : '(\[$@%]@)') . <<'SUB_READONLY'; { if (ref $_[0] eq 'SCALAR') { croak $MODIFY if is_sv_readonly ${$_[0]}; my $badtype = _is_badtype (ref tied ${$_[0]}); croak "$REASSIGN $badtype" if $badtype; croak "Readonly scalar must have only one value" if @_ > 2; my $tieobj = eval {tie ${$_[0]}, 'Readonly::Scalar', $_[1]}; # Tie may have failed because user tried to tie a constant, or we screwed up somehow. if ($@) { croak $MODIFY if $@ =~ /^$MODIFY at/; # Point the finger at the user. die "$@\n"; # Not a modify read-only message; must be our fault. } return $tieobj; } elsif (ref $_[0] eq 'ARRAY') { my $aref = shift; return Array @$aref, @_; } elsif (ref $_[0] eq 'HASH') { my $href = shift; croak $ODDHASH if @_%2 != 0 && !(@_ == 1 && ref $_[0] eq 'HASH'); return Hash %$href, @_; } elsif (ref $_[0]) { croak "Readonly only supports scalar, array, and hash variables."; } else { croak "First argument to Readonly must be a reference."; } } SUB_READONLY 1; =head1 NAME Readonly - Facility for creating read-only scalars, arrays, hashes =head1 Synopsis use Readonly; # Deep Read-only scalar Readonly::Scalar $sca => $initial_value; Readonly::Scalar my $sca => $initial_value; # Deep Read-only array Readonly::Array @arr => @values; Readonly::Array my @arr => @values; # Deep Read-only hash Readonly::Hash %has => (key => value, key => value, ...); Readonly::Hash my %has => (key => value, key => value, ...); # or: Readonly::Hash %has => {key => value, key => value, ...}; # You can use the read-only variables like any regular variables: print $sca; $something = $sca + $arr[2]; next if $has{$some_key}; # But if you try to modify a value, your program will die: $sca = 7; push @arr, 'seven'; delete $has{key}; # The error message is "Modification of a read-only value attempted" # Alternate form (Perl 5.8 and later) Readonly $sca => $initial_value; Readonly my $sca => $initial_value; Readonly @arr => @values; Readonly my @arr => @values; Readonly %has => (key => value, key => value, ...); Readonly my %has => (key => value, key => value, ...); Readonly my $sca; # Implicit undef, readonly value # Alternate form (for Perls earlier than v5.8) Readonly \$sca => $initial_value; Readonly \my $sca => $initial_value; Readonly \@arr => @values; Readonly \my @arr => @values; Readonly \%has => (key => value, key => value, ...); Readonly \my %has => (key => value, key => value, ...); =head1 Description This is a facility for creating non-modifiable variables. This is useful for configuration files, headers, etc. It can also be useful as a development and debugging tool for catching updates to variables that should not be changed. =head1 Variable Depth Readonly has the ability to create both deep and shallow readonly variables. If any of the values you pass to C, C, C, or the standard C are references, then those functions recurse over the data structures, marking everything as Readonly. The entire structure is nonmodifiable. This is normally what you want. If you want only the top level to be Readonly, use the alternate (and poorly named) C, C, and C functions. =head1 =head1 The Past The following sections are updated versions of the previous authors documentation. =head2 Comparison with "use constant" Perl provides a facility for creating constant values, via the L pragma. There are several problems with this pragma. =over 2 =item * The constants created have no leading sigils. =item * These constants cannot be interpolated into strings. =item * Syntax can get dicey sometimes. For example: use constant CARRAY => (2, 3, 5, 7, 11, 13); $a_prime = CARRAY[2]; # wrong! $a_prime = (CARRAY)[2]; # right -- MUST use parentheses =item * You have to be very careful in places where barewords are allowed. For example: use constant SOME_KEY => 'key'; %hash = (key => 'value', other_key => 'other_value'); $some_value = $hash{SOME_KEY}; # wrong! $some_value = $hash{+SOME_KEY}; # right (who thinks to use a unary plus when using a hash to scalarize the key?) =item * C works for scalars and arrays, not hashes. =item * These constants are global to the package in which they're declared; cannot be lexically scoped. =item * Works only at compile time. =item * Can be overridden: use constant PI => 3.14159; ... use constant PI => 2.71828; (this does generate a warning, however, if you have warnings enabled). =item * It is very difficult to make and use deep structures (complex data structures) with C. =back =head1 Comparison with typeglob constants Another popular way to create read-only scalars is to modify the symbol table entry for the variable by using a typeglob: *a = \'value'; This works fine, but it only works for global variables ("my" variables have no symbol table entry). Also, the following similar constructs do B work: *a = [1, 2, 3]; # Does NOT create a read-only array *a = { a => 'A'}; # Does NOT create a read-only hash =head2 Pros Readonly.pm, on the other hand, will work with global variables and with lexical ("my") variables. It will create scalars, arrays, or hashes, all of which look and work like normal, read-write Perl variables. You can use them in scalar context, in list context; you can take references to them, pass them to functions, anything. Readonly.pm also works well with complex data structures, allowing you to tag the whole structure as nonmodifiable, or just the top level. Also, Readonly variables may not be reassigned. The following code will die: Readonly::Scalar $pi => 3.14159; ... Readonly::Scalar $pi => 2.71828; =head2 Cons Readonly.pm used to impose a performance penalty. It was pretty slow. How slow? Run the C script that comes with Readonly. On my test system, "use constant" (const), typeglob constants (tglob), regular read/write Perl variables (normal/literal), and the new Readonly (ro/ro_simple) are all about the same speed, the old, tie based Readonly.pm constants were about 1/22 the speed. However, there is relief. There is a companion module available, Readonly::XS. You won't need this if you're using Perl 5.8.x or higher. I repeat, you do not need Readonly::XS if your environment has perl 5.8.x or higher. Please see section entitled L for more. =head1 Functions =over 4 =item Readonly::Scalar $var => $value; Creates a nonmodifiable scalar, C<$var>, and assigns a value of C<$value> to it. Thereafter, its value may not be changed. Any attempt to modify the value will cause your program to die. A value I be supplied. If you want the variable to have C as its value, you must specify C. If C<$value> is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the C<$value> marked as Readonly, use C. If $var is already a Readonly variable, the program will die with an error about reassigning Readonly variables. =item Readonly::Array @arr => (value, value, ...); Creates a nonmodifiable array, C<@arr>, and assigns the specified list of values to it. Thereafter, none of its values may be changed; the array may not be lengthened or shortened or spliced. Any attempt to do so will cause your program to die. If any of the values passed is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the hash C<%@arr> itself marked as Readonly, use C. If C<@arr> is already a Readonly variable, the program will die with an error about reassigning Readonly variables. =item Readonly::Hash %h => (key => value, key => value, ...); =item Readonly::Hash %h => {key => value, key => value, ...}; Creates a nonmodifiable hash, C<%h>, and assigns the specified keys and values to it. Thereafter, its keys or values may not be changed. Any attempt to do so will cause your program to die. A list of keys and values may be specified (with parentheses in the synopsis above), or a hash reference may be specified (curly braces in the synopsis above). If a list is specified, it must have an even number of elements, or the function will die. If any of the values is a reference to a scalar, array, or hash, then this function will mark the scalar, array, or hash it points to as being Readonly as well, and it will recursively traverse the structure, marking the whole thing as Readonly. Usually, this is what you want. However, if you want only the hash C<%h> itself marked as Readonly, use C. If C<%h> is already a Readonly variable, the program will die with an error about reassigning Readonly variables. =item Readonly $var => $value; =item Readonly @arr => (value, value, ...); =item Readonly %h => (key => value, ...); =item Readonly %h => {key => value, ...}; =item Readonly $var; The C function is an alternate to the C, C, and C functions. It has the advantage (if you consider it an advantage) of being one function. That may make your program look neater, if you're initializing a whole bunch of constants at once. You may or may not prefer this uniform style. It has the disadvantage of having a slightly different syntax for versions of Perl prior to 5.8. For earlier versions, you must supply a backslash, because it requires a reference as the first parameter. Readonly \$var => $value; Readonly \@arr => (value, value, ...); Readonly \%h => (key => value, ...); Readonly \%h => {key => value, ...}; You may or may not consider this ugly. Note that you can create implicit undefined variables with this function like so C while a verbose undefined value must be passed to the standard C, C, and C functions. =item Readonly::Scalar1 $var => $value; =item Readonly::Array1 @arr => (value, value, ...); =item Readonly::Hash1 %h => (key => value, key => value, ...); =item Readonly::Hash1 %h => {key => value, key => value, ...}; These alternate functions create shallow Readonly variables, instead of deep ones. For example: Readonly::Array1 @shal => (1, 2, {perl=>'Rules', java=>'Bites'}, 4, 5); Readonly::Array @deep => (1, 2, {perl=>'Rules', java=>'Bites'}, 4, 5); $shal[1] = 7; # error $shal[2]{APL}='Weird'; # Allowed! since the hash isn't Readonly $deep[1] = 7; # error $deep[2]{APL}='Weird'; # error, since the hash is Readonly =back =head1 Examples These are a few very simple examples: =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; =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 => (); Readonly::Array @a; # equivalent =head2 Hashes Typical usage: Readonly::Hash %a => (key1 => 'value1', key2 => 'value2'); A read-only hash can be initialized from a variable one: Readonly::Hash %a => %computed_values; A read-only hash can be empty: Readonly::Hash %a => (); Readonly::Hash %a; # equivalent If you pass an odd number of values, the program will die: Readonly::Hash %a => (key1 => 'value1', "value2"); # This dies with "May not store an odd number of values in a hash" =head1 Exports Historically, this module exports the C symbol into the calling program's namespace by default. The following symbols are also available for import into your program, if you like: C, C, C, C, C, and C. =head1 Internals Some people simply do not understand the relationship between this module and Readonly::XS so I'm adding this section. Odds are, they still won't understand but I like to write so... In the past, Readonly's "magic" was performed by C-ing variables to the C, C, and C packages (not to be confused with the functions of the same names) and acting on C, C, et. al. While this worked well, it was slow. Very slow. Like 20-30 times slower than accessing variables directly or using one of the other const-related modules that have cropped up since Readonly was released in 2003. To 'fix' this, Readonly::XS was written. If installed, Readonly::XS used the internal methods C and C to lock simple scalars. On the surface, everything was peachy but things weren't the same behind the scenes. In edge cases, code perfromed very differently if Readonly::XS was installed and because it wasn't a required dependency in most code, it made downstream bugs very hard to track. In the years since Readonly::XS was released, the then private internal methods have been exposed and can be used in pure perl. Similar modules were written to take advantage of this and a patch to Readonly was created. We no longer need to build and install another module to make Readonly useful on modern builds of perl. =over =item * You do not need to install Readonly::XS. =item * You should stop listing Readonly::XS as a dependency or expect it to be installed. =item * Stop testing the C<$Readonly::XSokay> variable! =back =head1 Requirements Please note that most users of Readonly no longer need to install the companion module Readonly::XS which is recommended but not required for perl 5.6.x and under. Please do not force it as a requirement in new code and do not use the package variable C<$Readonly::XSokay> in code/tests. For more, see L. 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. Please check the TODO file included with this distribution in case your bug is already known (...I probably won't file bug reports to myself). =head1 Acknowledgements Thanks to Slaven Rezic for the idea of one common function (Readonly) for all three types of variables (13 April 2002). Thanks to Ernest Lergon for the idea (and initial code) for deeply-Readonly data structures (21 May 2002). Thanks to Damian Conway for the idea (and code) for making the Readonly function work a lot smoother under perl 5.8+. =head1 Author Sanko Robinson - http://sankorobinson.com/ CPAN ID: SANKO Original author: Eric J. Roode, roode@cpan.org =head1 License and Legal Copyright (C) 2013, 2014 by Sanko Robinson Copyright (c) 2001-2004 by Eric J. Roode. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Readonly-2.00/META.yml0000644000000000000000000000210412354243333013156 0ustar rootroot--- abstract: 'Facility for creating read-only scalars, arrays, hashes' author: - 'Sanko Robinson - http://sankorobinson.com/' build_requires: Test::More: '0' configure_requires: CPAN::Meta: '0' CPAN::Meta::Prereqs: '0' Module::Build: '0.38' dynamic_config: 0 generated_by: 'Minilla/v1.1.0, CPAN::Meta::Converter version 2.141170' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Readonly no_index: directory: - t - xt - inc - share - eg - examples - author - builder provides: Readonly: file: lib/Readonly.pm version: '2.00' Readonly::Array: file: lib/Readonly.pm Readonly::Hash: file: lib/Readonly.pm Readonly::Scalar: file: lib/Readonly.pm recommends: perl: v5.20.0 requires: perl: v5.6.0 resources: bugtracker: https://github.com/sanko/readonly/issues homepage: https://github.com/sanko/readonly repository: git://github.com/sanko/readonly.git version: '2.00' x_contributors: - 'David Steinbrunner '