File Coverage

blib/lib/Array/Lock.pm
Criterion Covered Total %
statement 6 19 31.5
branch 2 4 50.0
condition n/a
subroutine 2 7 28.5
pod 3 6 50.0
total 13 36 36.1


line stmt bran cond sub pod time code
1             package Array::Lock;
2             require 5.007003;
3             # use strict;
4             # use warnings;
5             require Exporter;
6             our @ISA = qw(Exporter);
7             our %EXPORT_TAGS = ( 'all' => [ qw(lock_values unlock_values lock_indexes
8             unlock_indexes lock_array unlock_array) ] );
9             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
10             our $VERSION = '0.02';
11              
12             =head1 NAME
13              
14             Array::Lock- Subroutines to make Arrays read-only.
15              
16             =head1 SYNOPSIS
17              
18             use Array::Lock qw(lock_indexes unlock_indexes
19             lock_value unlock_value
20             lock_array unlock_array);
21              
22             @array = qw/f o o b a r/;
23             @indexes = qw/1 2 4/;
24             lock_values (@array);
25             lock_values (@array, @indexes);
26             unlock_values (@array);
27              
28             lock_indexes (@array);
29             unlock_indexes (@array);
30              
31             lock_array (@array);
32             unlock_array (@array);
33              
34             =head1 DESCRIPTION
35              
36             C contains functions to lock an array.
37              
38             By default C does not export anything.
39              
40             =head2 Restricted arrays
41              
42             Perl 5.8.0 (inadvertantly for arrays?) introduces the ability to restrict
43             an array to a range of indexes... No indexes outside of these can be
44             altered.. It also introduces the ability to lock an individual index so
45             it cannot be deleted and the value cannot be changed.
46              
47             =over 4
48              
49             =item lock_indexes
50              
51             =item unlock_indexxes
52              
53             lock_indexes(@array);
54              
55             Restricts the given arrays indexes to its current amount. No more indexes
56             can be added; however, the values of current indexes can be changed.
57             exists() will still work, but delete() will not, as its standard behavior
58             is to get rid of the current index. B: the current implementation prevents
59             bless()ing while locked. Any attempt to do so will raise an exception. Of course
60             you can still bless() the array before you call lock_indexes() so this shouldn't be
61             a problem.
62              
63             Right now, lock_indexes does not function with a range. However, if I get feedback that
64             sugests that a range is desired, a hack of some sort may be possible.
65              
66             unlock_indexes(@array);
67              
68             Removes the restriction on the array's indexes.
69              
70             =cut
71 0     0 1 0 sub lock_indexes (\@) { Internals::SvREADONLY @{$_[0]}, 1; }
  0         0  
72 0     0 0 0 sub unlock_indexes (\@) { Internals::SvREADONLY @{$_[0]}, 0; }
  0         0  
73             # You cannot lock a specific index, because of shift...
74             # I guess, you could lock that one index, and allow all the other
75             # indexes _above_ it to be usable... should I do that?
76              
77             =item lock_value
78              
79             =item unlock_value
80              
81             lock_values (@array, @indexes);
82             lock_values (@array);
83             unlock_values (@array, @indexes);
84              
85             Locks and unlocks index value pairs in an array. If no set of indexes is
86             specified, then all current indexes are locked.
87              
88             =cut
89              
90             sub lock_values (\@;@) {
91 2     2 0 147 my($array,@indexes) = @_;
92 1 100   1   6806 Internals::SvREADONLY $array->[$_], 1 for @indexes ? @indexes : $[.. $#{$array};
  1         410  
  1         243  
  2         10  
  1         16  
93             }
94              
95             sub unlock_values (\@;@) {
96 0     0 0   my($array,@indexes) = @_;
97 0 0         Internals::SvREADONLY $array->[$_], 0 for @indexes ? @indexes : $[.. $#{$array};
  0            
98             }
99              
100             =item B
101              
102             =item B
103              
104             lock_array(@array);
105              
106             lock_array() locks an entire array, making all indexes and values readonly.
107             No value can be changed, no indexes can be added or deleted.
108              
109             unlock_array(@array);
110              
111             unlock_arrray() does the opposite of lock_array(). All indexes and values
112             are made read/write. All values can be changed and indexes can be added
113             and deleted.
114              
115             =cut
116              
117             sub lock_array (\@) { #You can only retrieve from the array
118 0     0 1   my $array = shift;
119 0           lock_indexes(@$array);
120 0           lock_values(@$array);
121             }
122              
123             sub unlock_array (\@) {
124 0     0 1   my $array = shift;
125 0           unlock_indexes(@$array);
126 0           unlock_values(@$array);
127             }
128             1;
129             __END__