| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | Data::Plist::Writer - Object serializer and abstact | ||||||||||||||||||||||||||||||||||||||||||
| 4 | superclass for BinaryWriter and XMLWriter | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | # Create new | ||||||||||||||||||||||||||||||||||||||||||
| 9 | my $write = Data::Plist::BinaryWriter->new; | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # Writing to a string ($ret is binary output) | ||||||||||||||||||||||||||||||||||||||||||
| 12 | my $ret = $write->write($data); | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # Writing to a file C<$filename> | ||||||||||||||||||||||||||||||||||||||||||
| 15 | $ret = $write->write($filename, $data); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | C<Data::Plist::Writer> is the abstract superclass of | ||||||||||||||||||||||||||||||||||||||||||
| 20 | L<Data::Plist::BinaryWriter> and L<Data::Plist::XMLWriter>. It takes | ||||||||||||||||||||||||||||||||||||||||||
| 21 | perl data structures, serializes them (see L<Data::Plist/SERIALIZED | ||||||||||||||||||||||||||||||||||||||||||
| 22 | DATA>), and recursively writes to a given filehandle in the desired | ||||||||||||||||||||||||||||||||||||||||||
| 23 | format. | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | package Data::Plist::Writer; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use Storable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 5 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | Creates a new writer. 'serialize' is set to 1 since it's | ||||||||||||||||||||||||||||||||||||||||||
| 42 | assumed that data being passed in will be perl data | ||||||||||||||||||||||||||||||||||||||||||
| 43 | structures that need to be serialized. Users may set it to | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 when creating a writer if they wish to use preserialized | ||||||||||||||||||||||||||||||||||||||||||
| 45 | data. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 39 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 39 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 39 | my %args = ( serialize => 1, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 39 | return bless \%args => $class; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =head2 write $filehandle, $data | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | =head2 write $filename, $data | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | =head2 write $data | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | Takes a perl data structure C<$data> and writes to the given | ||||||||||||||||||||||||||||||||||||||||||
| 62 | filehandle C<$filehandle>, or filename C<$filename>. If only the | ||||||||||||||||||||||||||||||||||||||||||
| 63 | C<$data> is provided, returns the data to be written, as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 40 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=6 SCAR=24.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 40 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 40 | my $object = pop; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 40 | my $to = shift; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 40 | ▶ | if ( not $to ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 40 | my $content = ''; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 40 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 40 | open( $fh, ">", \$content ); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 40 | ▶ | $self->write_fh( $fh, $object ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 38 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } elsif ( ref $to ) { | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $self->write_fh( $to, $object ); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | open( $fh, ">", $to ) or die "Can't open $to for writing: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $self->write_fh( $fh, $object ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =head2 fold_uids $data | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | Takes a serialized object C<$data> (see | ||||||||||||||||||||||||||||||||||||||||||
| 91 | L<Data::Plist/SERIALIZED DATA>) and rewrites it as a keyed | ||||||||||||||||||||||||||||||||||||||||||
| 92 | archive (see L<Data::Plist/KEYED ARCHIVES>) by folding on | ||||||||||||||||||||||||||||||||||||||||||
| 93 | UIDs. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 11 | ▶ | sub fold_uids { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fold_uids: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 11 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 11 | ▶ | if ( $data->[0] eq "UID" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 3 | local $Storable::canonical = 1; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | my $digest = Digest::MD5::md5_hex( Storable::freeze( $data->[1] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | if ( exists $self->{objcache}{$digest} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | return [ UID => $self->{objcache}{$digest} ]; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | push @{ $self->{objects} }, $self->fold_uids( $data->[1] ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | $self->{objcache}{$digest} = @{ $self->{objects} } - 1; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | return [ UID => @{ $self->{objects} } - 1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } elsif ( $data->[0] eq "array" ) { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | return [ "array", [ map { $self->fold_uids($_) } @{ $data->[1] } ] ]; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } elsif ( $data->[0] eq "dict" ) { | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | my %dict = %{ $data->[1] }; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | $dict{$_} = $self->fold_uids( $dict{$_} ) for keys %dict; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | return [ "dict", \%dict ]; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 4 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =head2 serialize_value $data | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | Takes a perl data structure C<$data> and turns it into a | ||||||||||||||||||||||||||||||||||||||||||
| 124 | series of nested arrays of the format [datatype => data] | ||||||||||||||||||||||||||||||||||||||||||
| 125 | (see L<Data::Plist/SERIALIZED DATA>) in preparation for | ||||||||||||||||||||||||||||||||||||||||||
| 126 | writing. This is an internal data structure that should be | ||||||||||||||||||||||||||||||||||||||||||
| 127 | immediately handed off to a writer. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 350 | ▶ | sub serialize_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize_value: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 350 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 350 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 350 | ▶ | if ( not defined $value ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | return [ string => '$null' ]; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } elsif ( ref $value ) { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 16 | ▶ | if ( ref $value eq "ARRAY" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 5 | array => [ map { $self->serialize_value($_) } @{$value} ] ]; | |||||||||||||||||||||||||||||||||||||||||
| 140 | ▶ | } elsif ( ref $value and ref $value eq "HASH" ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $value and ref $value eq "HASH"
MC/DC50%1/2
ref $value and ref $value eq "HASH"
ref $value
ref $value eq "HASH"
Truth table66%2/3
A and B
A= ref $valueB= ref $value eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 7 | my %hash = %{$value}; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 7 | $hash{$_} = $self->serialize_value( $hash{$_} ) for keys %hash; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 7 | return [ dict => \%hash ]; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } elsif ( $value->isa("Data::Plist::Foundation::NSObject") ) { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 3 | return $value->serialize; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } elsif ( $value->isa("DateTime") ) { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | return [ date => $value->epoch - 978307200 | |||||||||||||||||||||||||||||||||||||||||
| 148 | + $value->nanosecond / 1e9 ]; | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | die "Can't serialize unknown ref @{[ref $value]}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } elsif ( $value =~ /^-?\d+$/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 311 | return [ integer => $value ]; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } elsif ( Scalar::Util::looks_like_number($value) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 3 | return [ real => $value ]; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } elsif ( $value =~ /\0/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | return [ data => $value ]; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 15 | return [ string => $value ]; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =head2 serialize $data | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | Takes a data structure C<$data> and determines what sort of | ||||||||||||||||||||||||||||||||||||||||||
| 166 | serialization it should go through. | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | Objects wishing to provide their own serializations should | ||||||||||||||||||||||||||||||||||||||||||
| 169 | have a 'serialize' method, which should return something in | ||||||||||||||||||||||||||||||||||||||||||
| 170 | the internal structure mentioned above (see also | ||||||||||||||||||||||||||||||||||||||||||
| 171 | L<Data::Plist/SERIALIZED DATA>). | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 22 | ▶ | sub serialize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 22 | my $object = shift; | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 22 | ▶ | return $self->serialize_value($object) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not ref $object or ref($object) =~ /ARRAY|HASH/ or not $object->can("serialize")
not ref $object or ref($object) =~ /ARRAY|HASH/
MC/DC100%3/3
not ref $object or ref($object) =~ /ARRAY|HASH/ or not $object->can("serialize")
not ref $object
ref($object) =~ /ARRAY|HASH/
not $object->can("serialize")
Truth table100%4/4
not(A) or B or not(C)
A= not ref $objectB= ref($object) =~ /ARRAY|HASH/C= not $object->can("serialize")
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | if not ref($object) | ||||||||||||||||||||||||||||||||||||||||||
| 181 | or ref($object) =~ /ARRAY|HASH/ | ||||||||||||||||||||||||||||||||||||||||||
| 182 | or not $object->can("serialize"); | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | $object = $object->serialize; | |||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | local $self->{objects} = []; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | local $self->{objcache} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | my $top = $self->fold_uids( [ dict => { root => [ UID => $object ] } ] ); | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 191 | dict => { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | '$archiver' => [ string => "NSKeyedArchiver" ], | ||||||||||||||||||||||||||||||||||||||||||
| 193 | '$version' => [ integer => 100_000 ], | ||||||||||||||||||||||||||||||||||||||||||
| 194 | '$top' => $top, | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | '$objects' => [ array => $self->{objects} ], | |||||||||||||||||||||||||||||||||||||||||
| 196 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 197 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||