| 1 | # $Author: domi $ | ||||||||||||||||||
| 2 | # $Date: 2004/12/08 12:50:41 $ | ||||||||||||||||||
| 3 | # $Name: $ | ||||||||||||||||||
| 4 | # $Revision: 1.3 $ | ||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | package Class::IntrospectionMethods::Parent ; | ||||||||||||||||||
| 7 | 16 | ▶ | use strict ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 16 | ▶ | use warnings ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 16 | ▶ | use Carp ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 16 | ▶ | use Storable qw/dclone/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 16 | ▶ | use Data::Dumper ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 16 | require Exporter; | |||||||||||||||||
| 14 | 16 | ▶ | use vars qw/$VERSION @ISA @EXPORT_OK $trace/ ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | 16 | @ISA = qw(Exporter); | |||||||||||||||||
| 16 | 16 | @EXPORT_OK = qw(set_parent_method_name graft_parent_method set_obsolete_behavior); | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 16 | $VERSION = sprintf "%d.%03d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 16 | $trace = 0; | |||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 16 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | Class::IntrospectionMethods::Parent - Handles parent relationship for Class::IntrospectionMethods | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | No synopsis. Directly used by Class::IntrospectionMethods | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | This class handles parent relationship for Class::IntrospectionMethods. | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | In other word, for any child object managed by | ||||||||||||||||||
| 37 | Class::IntrospectionMethods, it will : | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | =over | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | =item * | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | Create a ParentInfo object that contains | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | =over | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | =item * | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | the parent object ref (weakened by L<Scalar::Util> C<weaken> function) | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | =item * | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | The slot name containing the child | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | =item * | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | The index of the element containing the child if the slot is array or | ||||||||||||||||||
| 58 | hash based. | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | =back | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | =item * | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | Install a function/method in child's class to retrieve the | ||||||||||||||||||
| 65 | ParentInfo object. | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | =item * | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | An attribute in child to store the ParentInfo's ref. | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | =back | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | By default, the name of the installed function and arribute is | ||||||||||||||||||
| 74 | C<cim_parent> but this can be changed by calling | ||||||||||||||||||
| 75 | C<set_parent_method_name>. | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | =cut | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | 16 | my $obsolete_behavior = 'carp' ; | |||||||||||||||||
| 80 | 16 | my $support_legacy = 0 ; | |||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | sub warn_obsolete | ||||||||||||||||||
| 83 | 39 | ▶ | { | ||||||||||||||||
|
Subroutine
warn_obsolete: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||
| 84 | 39 | ▶ | return if $obsolete_behavior eq 'skip' ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | 16 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 86 | 0 | $obsolete_behavior->(@_) ; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | =head1 Exported functions | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | =head2 set_parent_method_name( name ) | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | This function changes the name of the function and attribute names | ||||||||||||||||||
| 94 | installed by C<graft_parent_method>. (C<cim_parent> by default) | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | =cut | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 16 | my $parent_method_name = 'cim_parent' ; | |||||||||||||||||
| 99 | 16 | my $too_late_to_change_name = 0 ; | |||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | sub set_parent_method_name | ||||||||||||||||||
| 102 | 2 | ▶ | { | ||||||||||||||||
|
Subroutine
set_parent_method_name: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||
| 103 | 2 | ▶ | croak "set_parent_method_name must be called before graft_parent_method" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | if $too_late_to_change_name ; | ||||||||||||||||||
| 105 | 2 | $parent_method_name = shift ; | |||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | sub set_obsolete_behavior | ||||||||||||||||||
| 109 | 16 | ▶ | { | ||||||||||||||||
|
Subroutine
set_obsolete_behavior: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 110 | 16 | $obsolete_behavior = shift; | |||||||||||||||||
| 111 | 16 | $support_legacy = shift ; | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | =head2 graft_parent_method( child, parent, slot, [index] ) | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | Creates the ParentInfo object, install the C<cim_parent> function in | ||||||||||||||||||
| 117 | child's class, store the ParentInfo in child object, finally store | ||||||||||||||||||
| 118 | slot and index in ParentInfo object. | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | =cut | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | # this function is called anytime a child object is created | ||||||||||||||||||
| 123 | sub graft_parent_method | ||||||||||||||||||
| 124 | 44 | ▶ | { | ||||||||||||||||
|
Subroutine
graft_parent_method: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||
| 125 | 44 | my ($child,$parent, $slot, $index) = @_ ; | |||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | 44 | $too_late_to_change_name = 1; | |||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | 44 | ▶ | croak "graft_parent_method error: cannot graft method if object is not based on HASH" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 130 | unless $child->isa('HASH') ; | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | 44 | my $parent_class = ref($parent) ; | |||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 44 | my $subname = ref($child).'::'.$parent_method_name ; | |||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 44 | ▶ | print "grafting child $subname with an accessor for parent $parent_class $parent\n". | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 137 | (defined $slot ? "\tslot is $slot\n" : '') . | ||||||||||||||||||
| 138 | (defined $index ? "\tindex is $index\n" : '' ) | ||||||||||||||||||
| 139 | if $trace ; | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | 16 | ▶ | no strict 'refs' ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 142 | *$subname = sub | ||||||||||||||||||
| 143 | 31 | ▶ | { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 144 | 31 | return shift -> {$parent_method_name} ; | |||||||||||||||||
| 145 | } | ||||||||||||||||||
| 146 | 44 | ▶ | unless $child -> can($parent_method_name) ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | 44 | my $parent_obj = $child->{$parent_method_name} = | |||||||||||||||||
| 149 | Class::IntrospectionMethods::ParentInfo | ||||||||||||||||||
| 150 | -> new( index_value => $index, | ||||||||||||||||||
| 151 | slot_name => $slot, | ||||||||||||||||||
| 152 | parent => $parent | ||||||||||||||||||
| 153 | ) ; | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | 44 | ▶ | if ($support_legacy) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 156 | { | ||||||||||||||||||
| 157 | tie $child->{CMM_SLOT_NAME} , | ||||||||||||||||||
| 158 | 42 | 'Class::IntrospectionMethods::ParentNameTie' , | |||||||||||||||||
| 159 | name => 'CMM_SLOT_NAME', | ||||||||||||||||||
| 160 | parent => $parent_obj , method => 'slot_name'; | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | tie $child->{CMM_INDEX_VALUE} , | ||||||||||||||||||
| 163 | 42 | 'Class::IntrospectionMethods::ParentNameTie', | |||||||||||||||||
| 164 | name => 'CMM_INDEX_VALUE', | ||||||||||||||||||
| 165 | parent => $parent_obj, method => 'index_value' ; | ||||||||||||||||||
| 166 | |||||||||||||||||||
| 167 | tie $child->{CMM_PARENT} , | ||||||||||||||||||
| 168 | 42 | 'Class::IntrospectionMethods::ParentNameTie', | |||||||||||||||||
| 169 | name => 'CMM_PARENT', | ||||||||||||||||||
| 170 | parent => $parent_obj, method => 'parent' ; | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | 42 | my $sub_slot_name = ref($child).'::CMM_SLOT_NAME' ; | |||||||||||||||||
| 173 | *$sub_slot_name = sub | ||||||||||||||||||
| 174 | 3 | ▶ | { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||
| 175 | 3 | warn_obsolete ("CMM_SLOT_NAME method is deprecated") ; | |||||||||||||||||
| 176 | 3 | my $po = shift ->$parent_method_name() ; | |||||||||||||||||
| 177 | 3 | ▶ | return defined $po ? $po->slot_name : undef; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 178 | 42 | ▶ | } unless $child -> can($sub_slot_name) ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | 42 | my $sub_index_name = ref($child).'::CMM_INDEX_VALUE' ; | |||||||||||||||||
| 181 | *$sub_index_name = sub | ||||||||||||||||||
| 182 | 3 | ▶ | { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||
| 183 | 3 | warn_obsolete ("CMM_INDEX_VALUE method is deprecated") ; | |||||||||||||||||
| 184 | 3 | my $po = shift ->$parent_method_name() ; | |||||||||||||||||
| 185 | 3 | ▶ | return defined $po ? $po->index_value :undef; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 186 | 42 | ▶ | } unless $child -> can($sub_index_name) ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | 42 | my $sub_parent = ref($child).'::CMM_PARENT' ; | |||||||||||||||||
| 189 | *$sub_parent = sub | ||||||||||||||||||
| 190 | 18 | ▶ | { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||
| 191 | 18 | warn_obsolete ("CMM_PARENT method is deprecated") ; | |||||||||||||||||
| 192 | 18 | my $po = shift ->$parent_method_name() ; | |||||||||||||||||
| 193 | 18 | ▶ | return defined $po ? ($po->parent(@_)) : (undef) ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 194 | 42 | ▶ | } unless $child -> can($sub_parent) ; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 195 | } | ||||||||||||||||||
| 196 | } | ||||||||||||||||||
| 197 | |||||||||||||||||||
| 198 | =head1 ParentInfo class | ||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | A ParentInfo object is created each time the C<graft_parent_method> | ||||||||||||||||||
| 201 | function is called. | ||||||||||||||||||
| 202 | |||||||||||||||||||
| 203 | When, needed, this object is retrieved by calling: | ||||||||||||||||||
| 204 | |||||||||||||||||||
| 205 | $child->cim_parent | ||||||||||||||||||
| 206 | |||||||||||||||||||
| 207 | The the following methods may be applied to retrive the informations | ||||||||||||||||||
| 208 | stored durung C<graft_parent_method> call: | ||||||||||||||||||
| 209 | |||||||||||||||||||
| 210 | =cut | ||||||||||||||||||
| 211 | |||||||||||||||||||
| 212 | package Class::IntrospectionMethods::ParentInfo ; | ||||||||||||||||||
| 213 | 16 | ▶ | use Scalar::Util qw(isweak weaken) ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 214 | |||||||||||||||||||
| 215 | sub new | ||||||||||||||||||
| 216 | 44 | ▶ | { | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 217 | 44 | my $type = shift; | |||||||||||||||||
| 218 | 44 | my $self = {@_ }; | |||||||||||||||||
| 219 | |||||||||||||||||||
| 220 | # Necessary to avoid ghost object and memory leaks. See | ||||||||||||||||||
| 221 | # WeakRef module See also "Programming perl" 3rd edition | ||||||||||||||||||
| 222 | # page 266. | ||||||||||||||||||
| 223 | 44 | weaken ($self -> {parent}) ; | |||||||||||||||||
| 224 | 44 | bless $self,$type ; | |||||||||||||||||
| 225 | } | ||||||||||||||||||
| 226 | |||||||||||||||||||
| 227 | =head2 index_value | ||||||||||||||||||
| 228 | |||||||||||||||||||
| 229 | Returns the index value of the element containing the child object. | ||||||||||||||||||
| 230 | Returns undex if the Class::IntrospectionMethods slot is not hash or | ||||||||||||||||||
| 231 | array based. | ||||||||||||||||||
| 232 | |||||||||||||||||||
| 233 | =cut | ||||||||||||||||||
| 234 | |||||||||||||||||||
| 235 | 7 | ▶ | sub index_value { return shift -> {index_value} ;} | ||||||||||||||||
|
Subroutine
index_value: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 236 | |||||||||||||||||||
| 237 | =head2 index_value | ||||||||||||||||||
| 238 | |||||||||||||||||||
| 239 | Identical to index_value. This method may be preferred for hash based | ||||||||||||||||||
| 240 | slots. (This is just syntactical sugar). | ||||||||||||||||||
| 241 | |||||||||||||||||||
| 242 | =cut | ||||||||||||||||||
| 243 | |||||||||||||||||||
| 244 | 0 | ▶ | sub key_name { return shift -> {index_value} ;} | ||||||||||||||||
|
Subroutine
key_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 245 | |||||||||||||||||||
| 246 | |||||||||||||||||||
| 247 | |||||||||||||||||||
| 248 | =head2 slot_name | ||||||||||||||||||
| 249 | |||||||||||||||||||
| 250 | Returns the name of the IntrospectionMethods slot containing the child | ||||||||||||||||||
| 251 | object. | ||||||||||||||||||
| 252 | |||||||||||||||||||
| 253 | =cut | ||||||||||||||||||
| 254 | |||||||||||||||||||
| 255 | 17 | ▶ | sub slot_name { return shift -> {slot_name} ;} | ||||||||||||||||
|
Subroutine
slot_name: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 256 | |||||||||||||||||||
| 257 | =head2 parent | ||||||||||||||||||
| 258 | |||||||||||||||||||
| 259 | Returns the parent object containing child. | ||||||||||||||||||
| 260 | |||||||||||||||||||
| 261 | =cut | ||||||||||||||||||
| 262 | |||||||||||||||||||
| 263 | sub parent | ||||||||||||||||||
| 264 | 24 | ▶ | { | ||||||||||||||||
|
Subroutine
parent: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 265 | 24 | my $self = shift ; | |||||||||||||||||
| 266 | 24 | my $parent = shift ; | |||||||||||||||||
| 267 | 24 | ▶ | if (defined $parent) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 268 | { | ||||||||||||||||||
| 269 | # Necessary to avoid ghost object and memory leaks. See | ||||||||||||||||||
| 270 | # WeakRef module See also "Programming perl" 3rd edition | ||||||||||||||||||
| 271 | # page 266. | ||||||||||||||||||
| 272 | 2 | weaken ($self -> {parent} = $parent) ; | |||||||||||||||||
| 273 | } | ||||||||||||||||||
| 274 | return $self->{parent} | ||||||||||||||||||
| 275 | 24 | } | |||||||||||||||||
| 276 | |||||||||||||||||||
| 277 | |||||||||||||||||||
| 278 | # This class is provided for backward compatibility for an older | ||||||||||||||||||
| 279 | # projet (the one that used a modified version of Class::MethodMaker) | ||||||||||||||||||
| 280 | # Do not use. | ||||||||||||||||||
| 281 | |||||||||||||||||||
| 282 | package Class::IntrospectionMethods::ParentNameTie ; | ||||||||||||||||||
| 283 | |||||||||||||||||||
| 284 | 16 | require Tie::Scalar; | |||||||||||||||||
| 285 | 16 | ▶ | use Carp ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 286 | 16 | ▶ | use vars qw/@ISA/ ; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 287 | |||||||||||||||||||
| 288 | 16 | @ISA = ('Tie::Scalar'); | |||||||||||||||||
| 289 | |||||||||||||||||||
| 290 | sub TIESCALAR | ||||||||||||||||||
| 291 | 126 | ▶ | { | ||||||||||||||||
|
Subroutine
TIESCALAR: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 292 | 126 | my $type = shift; | |||||||||||||||||
| 293 | 126 | my $self = { @_ } ; | |||||||||||||||||
| 294 | 126 | bless $self, $type; | |||||||||||||||||
| 295 | } | ||||||||||||||||||
| 296 | |||||||||||||||||||
| 297 | sub FETCH | ||||||||||||||||||
| 298 | 15 | ▶ | { | ||||||||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 299 | 15 | my $self = shift; | |||||||||||||||||
| 300 | 15 | Class::IntrospectionMethods::Parent::warn_obsolete("Reading directly $self->{name} is deprecated"); | |||||||||||||||||
| 301 | 15 | my $m = $self->{method} ; | |||||||||||||||||
| 302 | 15 | return $self->{parent}->$m(@_) | |||||||||||||||||
| 303 | } | ||||||||||||||||||
| 304 | |||||||||||||||||||
| 305 | sub STORE | ||||||||||||||||||
| 306 | 0 | ▶ | { | ||||||||||||||||
|
Subroutine
STORE: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 307 | 0 | my $self = shift; | |||||||||||||||||
| 308 | 0 | croak "Writing directly to $self->{name} is forbidden"; | |||||||||||||||||
| 309 | } | ||||||||||||||||||
| 310 | 16 | 1; | |||||||||||||||||
| 311 | |||||||||||||||||||
| 312 | __END__ | ||||||||||||||||||