| 1 | package Class::Storage; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # See perldoc at bottom of this file | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scalar::Util qw(blessed reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | our @EXPORT_OK = qw(packObjects unpackObjects); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use constant DEFAULT_TO_PACKED_METHOD_NAME => "TO_PACKED"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use constant DEFAULT_FROM_PACKED_METHOD_NAME => "FROM_PACKED"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # MooseX::Storage uses __CLASS__ and so it is perhaps a good idea not to choose | ||||||||||||||||||||||||||||
| 20 | # *exactly* the same magic string - then it isn't magic any more! :-) | ||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use constant DEFAULT_MAGIC_STRING => "__class__"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | sub packObjects { | ||||||||||||||||||||||||||
|
Subroutine
packObjects: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 6 | my ($data, %options) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 6 | _setDefaultOptions(\%options); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | my $val = _packObjects($data, \%options); | |||||||||||||||||||||||||||
| 29 | 6 | ▶ | return $val // $data; | ||||||||||||||||||||||||||
|
Condition33%1/3
$val // $data
MC/DC0%0/2
$val // $data
$val
$data
Truth table33%1/3
A // B
A= $valB= $data
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 22 | ▶ | sub _packObjects { | ||||||||||||||||||||||||||
|
Subroutine
_packObjects: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 33 | 22 | my ($data, $options) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 22 | my $toPackedMethodName = $options->{toPackedMethodName}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 22 | ▶ | if (blessed $data && $data->can($toPackedMethodName)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($data) and $data->can($toPackedMethodName)
MC/DC100%2/2
builtin::blessed($data) and $data->can($toPackedMethodName)
builtin::blessed($data)
$data->can($toPackedMethodName)
Truth table100%3/3
A and B
A= builtin::blessed($data)B= $data->can($toPackedMethodName)
| |||||||||||||||||||||||||||||
| 38 | 3 | my $packed = $data->$toPackedMethodName(); | |||||||||||||||||||||||||||
| 39 | 3 | bless $packed, ref($data); | |||||||||||||||||||||||||||
| 40 | 3 | $data = $packed; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 22 | ▶ | if (reftype $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 18 | ▶ | if (reftype $data eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 11 | return _packObjectsHash($data, $options); | |||||||||||||||||||||||||||
| 46 | } elsif (reftype $data eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 47 | 7 | return _packObjectsArray($data, $options); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | return undef; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 11 | ▶ | sub _packObjectsHash { | ||||||||||||||||||||||||||
|
Subroutine
_packObjectsHash: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 55 | 11 | my ($hash, $options) = @_; | |||||||||||||||||||||||||||
| 56 | # use Dbug; dbugDump(['hash', $hash]); | ||||||||||||||||||||||||||||
| 57 | 11 | foreach my $key (keys %$hash) { | |||||||||||||||||||||||||||
| 58 | 10 | my $val = $hash->{$key}; | |||||||||||||||||||||||||||
| 59 | 10 | my $newVal = _packObjects($val, $options); | |||||||||||||||||||||||||||
| 60 | 10 | ▶ | if ($newVal) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 8 | $hash->{$key} = $newVal; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 11 | ▶ | if (blessed $hash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | $hash = { | ||||||||||||||||||||||||||||
| 66 | ( $options->{magicString} ? | ||||||||||||||||||||||||||||
| 67 | 8 | ▶ | ( $options->{magicString} => ref($hash) ) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | %$hash | ||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 11 | return $hash; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | ▶ | sub _packObjectsArray { | ||||||||||||||||||||||||||
|
Subroutine
_packObjectsArray: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 75 | 7 | my ($array, $options) = @_; | |||||||||||||||||||||||||||
| 76 | # use Dbug; dbugDump(['array', $array]); | ||||||||||||||||||||||||||||
| 77 | 7 | foreach my $index (0..$#$array) { | |||||||||||||||||||||||||||
| 78 | 6 | my $val = $array->[$index]; | |||||||||||||||||||||||||||
| 79 | 6 | my $newVal = _packObjects($val, $options); | |||||||||||||||||||||||||||
| 80 | 6 | ▶ | if ($newVal) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4 | $array->[$index] = $newVal; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 7 | ▶ | if (blessed $array) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | $array = [ | ||||||||||||||||||||||||||||
| 86 | ( $options->{magicString} ? | ||||||||||||||||||||||||||||
| 87 | 5 | ▶ | ( $options->{magicString} => ref($array) ) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | @$array | ||||||||||||||||||||||||||||
| 89 | ]; | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 7 | return $array; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 5 | ▶ | sub unpackObjects { | ||||||||||||||||||||||||||
|
Subroutine
unpackObjects: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 5 | my ($data, %options) = @_; | |||||||||||||||||||||||||||
| 96 | 5 | _setDefaultOptions(\%options); | |||||||||||||||||||||||||||
| 97 | 5 | my $val = _unpackObjects($data, \%options); | |||||||||||||||||||||||||||
| 98 | 5 | ▶ | return $val // $data; | ||||||||||||||||||||||||||
|
Condition66%2/3
$val // $data
MC/DC0%0/2
$val // $data
$val
$data
Truth table66%2/3
A // B
A= $valB= $data
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 12 | ▶ | sub _unpackObjects { | ||||||||||||||||||||||||||
|
Subroutine
_unpackObjects: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 102 | 12 | my ($data, $options) = @_; | |||||||||||||||||||||||||||
| 103 | 12 | ▶ | if (reftype $data eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | 8 | return _unpackObjectsHash($data, $options); | |||||||||||||||||||||||||||
| 105 | } elsif (reftype $data eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 106 | 4 | return _unpackObjectsArray($data, $options); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | return undef; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 8 | ▶ | sub _unpackObjectsHash { | ||||||||||||||||||||||||||
|
Subroutine
_unpackObjectsHash: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 112 | 8 | my ($hash, $options) = @_; | |||||||||||||||||||||||||||
| 113 | 8 | my $class = delete $hash->{$options->{magicString}}; | |||||||||||||||||||||||||||
| 114 | 8 | ▶ | if ($class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 6 | my $fromPackedMethodName = $options->{fromPackedMethodName}; | |||||||||||||||||||||||||||
| 116 | 6 | ▶ | if ($class->can($fromPackedMethodName)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 2 | return $class->$fromPackedMethodName($hash); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 4 | bless $hash, $class; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 6 | foreach my $key (keys %$hash) { | |||||||||||||||||||||||||||
| 122 | 5 | my $newVal = _unpackObjects($hash->{$key}, $options); | |||||||||||||||||||||||||||
| 123 | 5 | ▶ | $hash->{$key} = $newVal | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | if defined $newVal; | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 6 | return undef; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 4 | ▶ | sub _unpackObjectsArray { | ||||||||||||||||||||||||||
|
Subroutine
_unpackObjectsArray: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 130 | 4 | my ($array, $options) = @_; | |||||||||||||||||||||||||||
| 131 | 4 | ▶ | if (scalar @$array >= 2 && $array->[0] eq $options->{magicString}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @$array >= 2 and $array->[0] eq $options->{"magicString"}
MC/DC50%1/2
scalar @$array >= 2 and $array->[0] eq $options->{"magicString"}
scalar @$array >= 2
$array->[0] eq $options->{"magicString"}
Truth table66%2/3
A and B
A= scalar @$array >= 2B= $array->[0] eq $options->{"magicString"}
| |||||||||||||||||||||||||||||
| 132 | 3 | shift @$array; | |||||||||||||||||||||||||||
| 133 | 3 | my $class = shift @$array; | |||||||||||||||||||||||||||
| 134 | 3 | my $fromPackedMethodName = $options->{fromPackedMethodName}; | |||||||||||||||||||||||||||
| 135 | 3 | ▶ | if ($class->can($fromPackedMethodName)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1 | return $class->$fromPackedMethodName($array); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 2 | bless $array, $class; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 3 | foreach my $i (0..$#$array) { | |||||||||||||||||||||||||||
| 141 | 2 | my $newVal = _unpackObjects($array->[$i], $options); | |||||||||||||||||||||||||||
| 142 | 2 | ▶ | $array->[$i] = $newVal | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | if defined $newVal; | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 3 | return undef; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 11 | ▶ | sub _setDefaultOptions { | ||||||||||||||||||||||||||
|
Subroutine
_setDefaultOptions: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 149 | 11 | my ($options) = @_; | |||||||||||||||||||||||||||
| 150 | 11 | ▶ | $options->{toPackedMethodName} //= DEFAULT_TO_PACKED_METHOD_NAME; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"toPackedMethodName"} //= "TO_PACKED"
MC/DC100%1/1
$options->{"toPackedMethodName"} //= "TO_PACKED"
$options->{"toPackedMethodName"}
Truth table100%2/2
A
A= $options->{"toPackedMethodName"}
| |||||||||||||||||||||||||||||
| 151 | 11 | ▶ | $options->{fromPackedMethodName} //= DEFAULT_FROM_PACKED_METHOD_NAME; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"fromPackedMethodName"} //= "FROM_PACKED"
MC/DC100%1/1
$options->{"fromPackedMethodName"} //= "FROM_PACKED"
$options->{"fromPackedMethodName"}
Truth table100%2/2
A
A= $options->{"fromPackedMethodName"}
| |||||||||||||||||||||||||||||
| 152 | 11 | ▶ | if (! exists $options->{magicString}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 8 | $options->{magicString} = DEFAULT_MAGIC_STRING; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 NAME | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Class::Storage - pack objects by removing blessing so they can be unpacked back | ||||||||||||||||||||||||||||
| 160 | into objects again later. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Handles blessed HASHes and ARRAYs | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 VERSION | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Version 0.03 | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | This module came into existence out of the need to be able to send I<objects> | ||||||||||||||||||||||||||||
| 171 | over JSON. JSON does not allow any blessed references to be sent by default and | ||||||||||||||||||||||||||||
| 172 | if sent, provides no generic way to resurrect these objects again after | ||||||||||||||||||||||||||||
| 173 | decoding. This can now all be done like this: | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | use JSON; | ||||||||||||||||||||||||||||
| 176 | use Class::Storage qw(packObjects unpackObjects); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | my $object = bless { a => 1 }, 'MyModule'; | ||||||||||||||||||||||||||||
| 179 | my $packed = packObjects( $object ); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # $packed is now { __class__ => 'MyModule', a => 1 } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | print $writeHandle encode_json($packed), "\n"; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # And on the other "side": | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | my $jsonString = <$readHandle>; | ||||||||||||||||||||||||||||
| 188 | my $packed = decode_json($jsonString); | ||||||||||||||||||||||||||||
| 189 | my $unpackedObject = unpackObjects($packed); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # $unpacked is now bless { a => 1 }, 'MyModule' | ||||||||||||||||||||||||||||
| 192 | # Which is_deeply the same as $object that we started with | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | However, there is no JSON-specific functionality in this module whatsoever, | ||||||||||||||||||||||||||||
| 195 | only a way to cleanly remove the bless-ing in a way that reliably can be | ||||||||||||||||||||||||||||
| 196 | re-introduced later. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head2 Using a magic string | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | As you can see from the L</"SYNOPSIS">, we use a magic string (C<__class__> by | ||||||||||||||||||||||||||||
| 203 | default) to store the class information for HASHes and ARRAYs. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | So C<packObjects> turns: | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | bless { key => "value" }, "ModuleA"; | ||||||||||||||||||||||||||||
| 208 | bless [ "val1", "val2" ], "ModuleB"; | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | into: | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | { __class__ => 'ModuleA', key => "value" } | ||||||||||||||||||||||||||||
| 213 | [ "__class__", 'ModuleB', "val1", "val2" ] | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | C<unpackObjects> converts any hashes with the magic string as a key and any | ||||||||||||||||||||||||||||
| 216 | arrays with the magic string as the first element back to blessed references | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | This "magic string" can be given as an option (see L</"OPTIONS">), but if you | ||||||||||||||||||||||||||||
| 219 | cannot live with a magic string, you can also provide | ||||||||||||||||||||||||||||
| 220 | C<< magicString => undef >>. But then you won't be able to unpack that data and | ||||||||||||||||||||||||||||
| 221 | turn it back into objects. If this is your itch, you may actually want | ||||||||||||||||||||||||||||
| 222 | L<Data::Structure::Util> instead. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head2 Returns packed/unpacked data + modifies input argument | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | The valid data is returned. However, for speed, we also modify and re-use data | ||||||||||||||||||||||||||||
| 227 | from the input value. So don't rely on being able to reuse the C<$data> input | ||||||||||||||||||||||||||||
| 228 | for C<packObjects> and C<unpackObjects> after they've been called and don't | ||||||||||||||||||||||||||||
| 229 | modify them either. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | If you don't want your input modified: | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | use Storable qw(dclone); | ||||||||||||||||||||||||||||
| 234 | my $pristineData = somesub(); | ||||||||||||||||||||||||||||
| 235 | my $packed = packObjects(dclone($pristineData)); | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =head2 Inspiration | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | Class::Storage is inspired by L<MooseX::Storage> but this is a generic | ||||||||||||||||||||||||||||
| 240 | implementation that works on all plain perl classes that are implemented as | ||||||||||||||||||||||||||||
| 241 | blessed references to HASHes and ARRAYs (B<only> hashes and arrays). | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | NOTE: L<MooseX::Storage> uses C<__CLASS__> as its magic string and we use | ||||||||||||||||||||||||||||
| 244 | C<__class__> to make sure they're not the same. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head2 C<TO_PACKED> and C<FROM_PACKED> | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | If you want to control how internal state gets represeted when packed, then | ||||||||||||||||||||||||||||
| 249 | provide a C<TO_PACKED> instance method. It will be called like: | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | my $packed = $object->TO_PACKED(); | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | This C<$packed> data will be used by C<packObjects> instead of the guts of | ||||||||||||||||||||||||||||
| 254 | C<$object>. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Similarly, during C<unpackObjects>, if a module has a C<FROM_PACKED> static | ||||||||||||||||||||||||||||
| 257 | method it will be called like this: | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | my $object = $module->FROM_PACKED($packed); | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | As you can see, C<TO_PACKED> and C<FROM_PACKED> go together as pairs. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | You can also modify the names of these methods with the C<toPackedMethodName> | ||||||||||||||||||||||||||||
| 264 | and C<fromPackedMethodName> options. See L</"OPTIONS>. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =head1 NOTE ABOUT KINDS OF BLESSED OBJECTS | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | L<perlobj> says: | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | "... it's possible to bless any type of data structure or referent, including | ||||||||||||||||||||||||||||
| 271 | scalars, globs, and subroutines. You may see this sort of thing when looking at | ||||||||||||||||||||||||||||
| 272 | code in the wild." | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | In particular I've seen several XS modules create instances where the internal | ||||||||||||||||||||||||||||
| 275 | state is not visible to Perl, and hence cannot be handled properly by this | ||||||||||||||||||||||||||||
| 276 | module. Here is an example with JSON: | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | use Data::Dumper; | ||||||||||||||||||||||||||||
| 279 | use JSON; | ||||||||||||||||||||||||||||
| 280 | print Dumper(JSON->new()->pretty(1)); | ||||||||||||||||||||||||||||
| 281 | # prints | ||||||||||||||||||||||||||||
| 282 | # $VAR1 = bless( do{\(my $o = '')}, 'JSON' ); | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | Clearly a L<JSON> object has internal state and other data. This is an example | ||||||||||||||||||||||||||||
| 285 | of a blessed reference, but not a blessed HASH or ARRAY that Class::Storage can | ||||||||||||||||||||||||||||
| 286 | handle. If you try C<packObjects>-ing such a JSON instance, Class::Storage will | ||||||||||||||||||||||||||||
| 287 | just leave the JSON object altogether untouched. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | our @EXPORT_OK = qw(packObjects unpackObjects); | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | Both C<packObjects> and C<unpackObjects> share the same C<%options>. See | ||||||||||||||||||||||||||||
| 296 | L</"OPTIONS"> below. | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head2 packObjects | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | my $packed = packObjects($blessed, %options); | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =head2 unpackObjects | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | my $unpacked = unpackObjects($unbessed, %options); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | These options are common to C<packObjects> and C<unpackObjects>: | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =over 4 | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =item * C<toPackedMethodName> | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | This option lets you change the name of the C<TO_UNBLESSED> method to something | ||||||||||||||||||||||||||||
| 315 | else. Hint: C<TO_JSON> could be a good idea here! | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =item * C<fromPackedMethodName> | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | This option lets you change the name of the C<TO_BLESSED> method to something | ||||||||||||||||||||||||||||
| 320 | else. Hint: C<FROM_JSON> could be a good idea here, even though L<JSON> | ||||||||||||||||||||||||||||
| 321 | doesn't have such a method. Which is actually the entire Raison d'Etre of this | ||||||||||||||||||||||||||||
| 322 | module! | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =item * C<magicString> | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Change the magic string used to store the class name to something else than | ||||||||||||||||||||||||||||
| 327 | C<__class__>. | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | If this is false, don't store class information at all, in which case | ||||||||||||||||||||||||||||
| 330 | C<packObjects> becomes analogous to L<Data::Structure::Util::packObjects>. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =back | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Peter Valdemar Mørch, C<< <peter@morch.com> >> | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =head1 BUGS | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 343 | L<https://github.com/pmorch/perl-Class-Storage/issues>. I will be notified, and | ||||||||||||||||||||||||||||
| 344 | then you'll automatically be notified of progress on your bug as I make | ||||||||||||||||||||||||||||
| 345 | changes. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | perldoc Class::Storage | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | You can also look for information at: | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =over 4 | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =item * Repository and Bug Tracker on Github | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | L<https://github.com/pmorch/perl-Class-Storage> | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | L<http://annocpan.org/dist/Class-Storage> | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | L<http://cpanratings.perl.org/d/Class-Storage> | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | L<http://search.cpan.org/dist/Class-Storage/> | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | =back | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | This has been inspired by many sources, but checkout: | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =over 4 | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =item * How to convert Perl objects into JSON and vice versa - Stack Overflow | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | L<http://stackoverflow.com/questions/4185482/how-to-convert-perl-objects-into-json-and-vice-versa/4185679> | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =item * How do I turn Moose objects into JSON for use in Catalyst? | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | L<http://stackoverflow.com/questions/3391967/how-do-i-turn-moose-objects-into-json-for-use-in-catalyst> | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =item * MooseX-Storage | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | L<https://metacpan.org/release/MooseX-Storage> | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =item * Brian D Foy's quick hack | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | Where he defines a TO_JSON in UNIVERSAL so it applies to all objects. It makes | ||||||||||||||||||||||||||||
| 396 | a deep copy, unblesses it, and returns the data structure. | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | L<http://stackoverflow.com/a/2330077/345716> | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =back | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | Copyright 2015 Peter Valdemar Mørch. | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 407 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 408 | copy of the full license at: | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||||||||||||||||
| 413 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||||||||||||||||
| 414 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||||||||||||||||
| 415 | or distribute the Package, if you do not accept this license. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||||||||||||||||
| 418 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||||||||||||||||
| 419 | your Modified Version complies with the requirements of this license. | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | This license does not grant you the right to use any trademark, service | ||||||||||||||||||||||||||||
| 422 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||||||||||||||||
| 425 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||||||||||||||||
| 426 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||||||||||||||||
| 427 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||||||||||||||||
| 428 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||||||||||||||||
| 429 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||||||||||||||||
| 430 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||||||||||||||||
| 431 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||||||||||||||||
| 434 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||||||||||||||||
| 435 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||
| 436 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||||||||||||||||
| 437 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||||||||||||||||
| 438 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||||||||||||||||
| 439 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||||||||||||||||
| 440 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =cut | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 3 | 1; # End of Class::Storage | |||||||||||||||||||||||||||