| 1 | package UR::DeletedRef; | ||||||||||
| 2 | |||||||||||
| 3 | 266 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 266 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 266 | require UR; | |||||||||
| 6 | |||||||||||
| 7 | 266 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.2
| |||||||||||
| 8 | # this is to workaround a Perl bug where the overload magic flag is not updated | ||||||||||
| 9 | # for references with a different RV (which happens anytime you do "my $object" | ||||||||||
| 10 | # https://rt.perl.org/rt3/Public/Bug/Display.html?id=9472 | ||||||||||
| 11 | 266 | ▶ | if ($^V lt v5.8.9) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 12 | 0 | eval "use overload fallback => 1"; | |||||||||
| 13 | }; | ||||||||||
| 14 | }; | ||||||||||
| 15 | |||||||||||
| 16 | 266 | our $VERSION = "0.47"; # UR $VERSION; | |||||||||
| 17 | |||||||||||
| 18 | 266 | our $all_objects_deleted = {}; | |||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | sub bury { | ||||||||
|
Subroutine
bury: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 21 | 0 | my $class = shift; | |||||||||
| 22 | |||||||||||
| 23 | 0 | for my $object (@_) { | |||||||||
| 24 | 0 | ▶ | if ($ENV{'UR_DEBUG_OBJECT_RELEASE'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | print STDERR "MEM BURY object $object class ",$object->class," id ",$object->id,"\n"; | |||||||||
| 26 | } | ||||||||||
| 27 | 0 | my $original_class = ref($object); | |||||||||
| 28 | 0 | my $original_id = $object->id; | |||||||||
| 29 | |||||||||||
| 30 | 0 | %$object = (original_class => ref($object), original_data => {%$object}); | |||||||||
| 31 | 0 | bless $object, 'UR::DeletedRef'; | |||||||||
| 32 | |||||||||||
| 33 | 0 | $all_objects_deleted->{$original_class}->{$original_id} = $object; | |||||||||
| 34 | 0 | Scalar::Util::weaken($all_objects_deleted->{$original_class}->{$original_id}); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 0 | return 1; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | sub resurrect { | ||||||||
|
Subroutine
resurrect: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 41 | 0 | ▶ | shift unless (ref($_[0])); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 0 | foreach my $object (@_) { | |||||||||
| 44 | 0 | my $original_class = $object->{'original_class'}; | |||||||||
| 45 | 0 | bless $object, $original_class; | |||||||||
| 46 | 0 | %$object = (%{$object->{original_data}}); | |||||||||
| 47 | 0 | my $id = $object->id; | |||||||||
| 48 | 0 | delete $all_objects_deleted->{$original_class}->{$id}; | |||||||||
| 49 | 0 | ▶ | $object->resurrect_object if ($object->can('resurrect_object')); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | return 1; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 266 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: not called CC=1 SCAR=6.9
| |||||||||||
| 58 | 0 | our $AUTOLOAD; | |||||||||
| 59 | 0 | my $method = $AUTOLOAD; | |||||||||
| 60 | 0 | $method =~ s/^.*:://g; | |||||||||
| 61 | 0 | Carp::croak("Attempt to use a reference to an object which has been deleted. A call was made to method '$method'\nRessurrect it first.\n" . Dumper($_[0])); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub __rollback__ { | ||||||||
|
Subroutine
__rollback__: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | 0 | return 1; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||
| 69 | 0 | ▶ | if ($ENV{'UR_DEBUG_OBJECT_RELEASE'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | print STDERR "MEM DESTROY deletedref $_[0]\n"; | |||||||||
| 71 | } | ||||||||||
| 72 | 0 | delete $all_objects_deleted->{"$_[0]"}; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 266 | 1; | |||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | =pod | ||||||||||
| 79 | |||||||||||
| 80 | =head1 NAME | ||||||||||
| 81 | |||||||||||
| 82 | UR::DeletedRef - Represents an instance of a no-longer-existent object | ||||||||||
| 83 | |||||||||||
| 84 | =head1 SYNOPSIS | ||||||||||
| 85 | |||||||||||
| 86 | my $obj = Some::Class->get(123); | ||||||||||
| 87 | |||||||||||
| 88 | $obj->delete; | ||||||||||
| 89 | print ref($obj),"\n"; # prints 'UR::DeletedRef' | ||||||||||
| 90 | $obj->some_method(); # generates an exception through Carp::confess | ||||||||||
| 91 | |||||||||||
| 92 | $obj->resurrect; | ||||||||||
| 93 | print ref($obj),"\n"; # prints 'Some::Class' | ||||||||||
| 94 | |||||||||||
| 95 | =head1 DESCRIPTION | ||||||||||
| 96 | |||||||||||
| 97 | Object instances become UR::DeletedRefs when some part of the application | ||||||||||
| 98 | calls delete() or unload() on them, meaning that they no longer exist | ||||||||||
| 99 | in that Context. The extant object reference is turned into a UR::DeletedRef | ||||||||||
| 100 | so that if that same reference is used in any capacity later in the program, | ||||||||||
| 101 | it will generate an exception through its AUTOLOAD to prevent using it by | ||||||||||
| 102 | mistake. | ||||||||||
| 103 | |||||||||||
| 104 | Note that UR::DeletedRef instances are different than Ghost objects. When a | ||||||||||
| 105 | UR-based object is deleted through delete(), a new Ghost object reference is | ||||||||||
| 106 | created from the data in the old object, and the old object reference is | ||||||||||
| 107 | re-blessed as a UR::DeletedRef. Any variables still referencing the original | ||||||||||
| 108 | object now hold a reference to this UR::DeletedRef. The Ghost object can be | ||||||||||
| 109 | retrieved by issuing a get() against the Ghost class. | ||||||||||
| 110 | |||||||||||
| 111 | Objects unloaded from the Context using unload(), or indirectly by rolling-back | ||||||||||
| 112 | a transaction which triggers unload of objects loaded during the transaction, | ||||||||||
| 113 | are also turned into UR::DeletedRefs. | ||||||||||
| 114 | |||||||||||
| 115 | You aren't likely to encounter UR::DeletedRefs in normal use. What usually | ||||||||||
| 116 | happens is that an object will be deleted with delete() (or unload()), the | ||||||||||
| 117 | lexical variable pointing to the DeletedRef will soon go out of scope and | ||||||||||
| 118 | the DeletedRef will then be garbage-colelcted. | ||||||||||
| 119 | |||||||||||
| 120 | =head1 SEE ALSO | ||||||||||
| 121 | |||||||||||
| 122 | UR::Object, UR::Object::Ghost, UR::Context | ||||||||||
| 123 | |||||||||||
| 124 | =cut | ||||||||||
| 125 | |||||||||||