| 1 | package Class::PublicPrivate; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | # version | ||||||||||||
| 5 | 1 | our $VERSION = '0.82'; | |||||||||||
| 6 | |||||||||||||
| 7 | #------------------------------------------------------------------------------ | ||||||||||||
| 8 | # opening POD | ||||||||||||
| 9 | # | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | Class::PublicPrivate - Class with public keys with any name and a separate set | ||||||||||||
| 14 | of private keys | ||||||||||||
| 15 | |||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||
| 17 | |||||||||||||
| 18 | PublicPrivate is intended for use as a base class for other classes. Users of | ||||||||||||
| 19 | class based on PublicPrivate can assign any keys to the object hash without | ||||||||||||
| 20 | interfering with keys used internally. The private data can be accessed by | ||||||||||||
| 21 | retrieving the private hash with the C<private> method. For example, the | ||||||||||||
| 22 | following code outputs two different values, one for the public value of | ||||||||||||
| 23 | C<start> and another for the private value of C<start>. | ||||||||||||
| 24 | |||||||||||||
| 25 | package ExtendedClass; | ||||||||||||
| 26 | use base 'Class::PublicPrivate'; | ||||||||||||
| 27 | |||||||||||||
| 28 | sub new{ | ||||||||||||
| 29 | my $class = shift; | ||||||||||||
| 30 | my $self = $class->SUPER::new(); | ||||||||||||
| 31 | my $private = $self->private; | ||||||||||||
| 32 | |||||||||||||
| 33 | # initialize one of the private properties | ||||||||||||
| 34 | $private->{'start'}=time(); | ||||||||||||
| 35 | |||||||||||||
| 36 | return $self; | ||||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | package main; | ||||||||||||
| 40 | my ($var); | ||||||||||||
| 41 | $var = ExtendedClass->new(); | ||||||||||||
| 42 | $var->{'start'} = 1; | ||||||||||||
| 43 | |||||||||||||
| 44 | print $var->{'start'}, "\n"; | ||||||||||||
| 45 | print $var->private()->{'start'}, "\n"; | ||||||||||||
| 46 | |||||||||||||
| 47 | =head1 INSTALLATION | ||||||||||||
| 48 | |||||||||||||
| 49 | Class::PublicPrivate can be installed with the usual routine: | ||||||||||||
| 50 | |||||||||||||
| 51 | perl Makefile.PL | ||||||||||||
| 52 | make | ||||||||||||
| 53 | make test | ||||||||||||
| 54 | make install | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 METHODS | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 YourClass->new(classname ,[initikey1=>initvalue [, ...]]) | ||||||||||||
| 59 | |||||||||||||
| 60 | Returns an instantiation of YourClass, where YourClass is a class that extends | ||||||||||||
| 61 | Class::PublicPrivate. Additional key=>value pairs are stored in the private | ||||||||||||
| 62 | hash. Programs that use your class can store any date directly in it w/o | ||||||||||||
| 63 | affecting the object's private data. | ||||||||||||
| 64 | |||||||||||||
| 65 | =head2 $ob->private() | ||||||||||||
| 66 | |||||||||||||
| 67 | Returns a reference to the hash of private data. | ||||||||||||
| 68 | |||||||||||||
| 69 | =head1 TERMS AND CONDITIONS | ||||||||||||
| 70 | |||||||||||||
| 71 | Copyright (c) 2015 by Miko O'Sullivan. All rights reserved. This program | ||||||||||||
| 72 | is free software; you can redistribute it and/or modify it under the same | ||||||||||||
| 73 | terms as Perl itself. This software comes with B<NO WARRANTY> of any kind. | ||||||||||||
| 74 | |||||||||||||
| 75 | =head1 AUTHOR | ||||||||||||
| 76 | |||||||||||||
| 77 | Miko O'Sullivan | ||||||||||||
| 78 | F<miko@idocs.com> | ||||||||||||
| 79 | |||||||||||||
| 80 | =cut | ||||||||||||
| 81 | |||||||||||||
| 82 | # | ||||||||||||
| 83 | # opening POD | ||||||||||||
| 84 | #------------------------------------------------------------------------------ | ||||||||||||
| 85 | |||||||||||||
| 86 | |||||||||||||
| 87 | #------------------------------------------------------------------------------ | ||||||||||||
| 88 | # new | ||||||||||||
| 89 | # | ||||||||||||
| 90 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 91 | 1 | my $class = shift; | |||||||||||
| 92 | 1 | my (%nv, $self); | |||||||||||
| 93 | |||||||||||||
| 94 | # reference nv in hash | ||||||||||||
| 95 | 1 | tie %nv, 'Class::PublicPrivate::Tie', @_; | |||||||||||
| 96 | 1 | $self = bless(\%nv, $class); | |||||||||||
| 97 | |||||||||||||
| 98 | # return | ||||||||||||
| 99 | 1 | return $self; | |||||||||||
| 100 | } | ||||||||||||
| 101 | # | ||||||||||||
| 102 | # new | ||||||||||||
| 103 | #------------------------------------------------------------------------------ | ||||||||||||
| 104 | |||||||||||||
| 105 | |||||||||||||
| 106 | #------------------------------------------------------------------------------ | ||||||||||||
| 107 | # private | ||||||||||||
| 108 | # returns the private hash | ||||||||||||
| 109 | # | ||||||||||||
| 110 | 2 | ▶ | sub private { | ||||||||||
|
Subroutine
private: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 111 | 2 | return (tied(%{$_[0]}))->{'private'}; | |||||||||||
| 112 | } | ||||||||||||
| 113 | # | ||||||||||||
| 114 | # private | ||||||||||||
| 115 | #------------------------------------------------------------------------------ | ||||||||||||
| 116 | |||||||||||||
| 117 | |||||||||||||
| 118 | |||||||||||||
| 119 | ############################################################################### | ||||||||||||
| 120 | # Class::PublicPrivate::Tie | ||||||||||||
| 121 | # | ||||||||||||
| 122 | package Class::PublicPrivate::Tie; | ||||||||||||
| 123 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 124 | |||||||||||||
| 125 | 1 | ▶ | sub TIEHASH { | ||||||||||
|
Subroutine
TIEHASH: called CC=3 SCAR=11.3
| |||||||||||||
| 126 | 1 | my ($class, %opts) = @_; | |||||||||||
| 127 | 1 | my $self = bless({}, $class); | |||||||||||
| 128 | |||||||||||||
| 129 | 1 | ▶ | $self->{'private'} = $opts{'private'} || {}; | ||||||||||
|
Condition50%1/2
$opts{'private'} || {}
MC/DC0%0/1
$opts{'private'} || {}
$opts{'private'}
Truth table50%1/2
A
A= $opts{'private'}
| |||||||||||||
| 130 | 1 | ▶ | $self->{'public'} = $opts{'public'} || {}; | ||||||||||
|
Condition50%1/2
$opts{'public'} || {}
MC/DC0%0/1
$opts{'public'} || {}
$opts{'public'}
Truth table50%1/2
A
A= $opts{'public'}
| |||||||||||||
| 131 | |||||||||||||
| 132 | 1 | return $self; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | 1 | ▶ | sub STORE { | ||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||
| 136 | 1 | $_[0]->{'public'}->{$_[1]} = $_[2]; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 0 | ▶ | sub FETCH { | ||||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||||
| 140 | 0 | return $_[0]->{'public'}->{$_[1]}; | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 0 | ▶ | sub DELETE { | ||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||
| 144 | 0 | delete $_[0]->{'public'}->{$_[1]}; | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 0 | ▶ | sub CLEAR { | ||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||
| 148 | 0 | $_[0]->{'public'} = {}; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | ▶ | sub EXISTS { | ||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||
| 152 | 0 | exists $_[0]->{'public'}->{$_[1]}; | |||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 1 | ▶ | sub FIRSTKEY { | ||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||
| 156 | 1 | my $self = shift; | |||||||||||
| 157 | 1 | my $a = keys(%{$self->{'public'}}); | |||||||||||
| 158 | 1 | return $self->NEXTKEY; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 2 | ▶ | sub NEXTKEY { | ||||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||||
| 162 | 2 | my $self = shift; | |||||||||||
| 163 | 2 | my $v = (each %{$self->{'public'}})[0]; | |||||||||||
| 164 | 2 | return $v; | |||||||||||
| 165 | } | ||||||||||||
| 166 | # | ||||||||||||
| 167 | # Class::PublicPrivate::Tie | ||||||||||||
| 168 | ############################################################################### | ||||||||||||
| 169 | |||||||||||||
| 170 | |||||||||||||
| 171 | |||||||||||||
| 172 | # return | ||||||||||||
| 173 | 1 | 1; | |||||||||||
| 174 | |||||||||||||
| 175 | =head1 VERSION | ||||||||||||
| 176 | |||||||||||||
| 177 | Version: | ||||||||||||
| 178 | |||||||||||||
| 179 | |||||||||||||
| 180 | =head1 HISTORY | ||||||||||||
| 181 | |||||||||||||
| 182 | =over | ||||||||||||
| 183 | |||||||||||||
| 184 | =item Version 0.80, June 29, 2002 | ||||||||||||
| 185 | |||||||||||||
| 186 | First public release | ||||||||||||
| 187 | |||||||||||||
| 188 | =item Version 0.81 May 18, 2014 | ||||||||||||
| 189 | |||||||||||||
| 190 | Minor tightening up of code. Fixed problems in packaging for CPAN. | ||||||||||||
| 191 | |||||||||||||
| 192 | =item Version 0.82 January 2, 2015 | ||||||||||||
| 193 | |||||||||||||
| 194 | Minor tidying up code formatting and POD. Modifed tests to include test names. | ||||||||||||
| 195 | Modifed files to use Unix style newlines, and to be encoded UTF-8. | ||||||||||||
| 196 | |||||||||||||
| 197 | =back | ||||||||||||
| 198 | |||||||||||||
| 199 | =cut | ||||||||||||