The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Plist::BinaryWriter - write binary property lists from Perl data structures

SYNOPSIS

 # Create new
 my $write = Data::Plist::BinaryWriter->new();

 # Writing to a string ($ret is binary output)
 my $ret = $write->write($data);

 # Writing to a file C<$filename>
 $write->write($filename, $data);

DESCRIPTION

Data::Plist::BinaryWriter takes perl data structures, serializes them (see "SERIALIZED DATA" in Data::Plist) and recursively writes to a given filehandle in Apple's binary property list format.

METHODS

write_fh $fh, $data

Takes a perl data structure $data, serializes it (see "SERIALIZED DATA" in Data::Plist) and writes it to the given filehandle $fh in Apple's binary property list format.

The format starts with "bplist00" and contains a 32-byte trailer. The 32-byte trailer consists of the size of the offset objects in the offset table, the size of the indices of the offset table, the number of objects in the binary file, the index of top object in the binary file and the offset table offset.

dispatch $data

Takes serialized data structure $data (see "SERIALIZED DATA" in Data::Plist) and checks its type. Checks the object against previously written objects. If no match is found, calls the appropriate write_ method. Returns the index into the offset table of the offset object that points to the data's position in the binary file.

make_type $type, $length

Takes a string representing the object's type $type and an integer indicating its size $length. Returns their binary representation.

Each object in the binary file is preceded by a byte - the higher nybble denoting its type and the lower its size. For objects whose size is equal to or great than 15, the lower byte contains an f and an integer object is added right after the first byte containing the object's actual size.

write_integer $int, $type

Takes an integer $int and an optional type $type (used for writing UIDs, since they're essentially the same). Returns the index into the offset table of the offset object that points to the integer's location in the binary file.

write_string $string

Takes a string $string and returns the index into the offset table of the offset object that points to its location in the binary file. It is encoded in the file using UTF-8.

write_ustring $ustring

Takes a string $ustring and returns the index into the offset table of the offset object that points to its location in the binary file.

While ustrings are technically supposed to be stored in UTF-16, there is no known reason for them to not be written as UTF-8 encoded strings instead; thus, for simplicity, all ustrings are written as strings.

write_dict $dict

Takes a hash reference $dict and recursively processes each of its keys and values. Stores indices into the offset table of the offset objects pointing to its keys and values in the binary file. Returns the index into the offset table of the offset object that points to its location in the binary file.

write_array $array

Take an array reference $array and recursively processes its contents. Stores the indices into the offset table of the offset objects pointing to its value. Returns the index into the offset table of the offset object that points to its location in the binary file.

write_UID $id

Takes a UID $id and returns the index into the offset table of the offset object that points to its location in the binary file. Passes the UID off to "write_integer" for actual writing, since they're processed in the same manner, simply with different types.

write_real $real, $type

Takes a float $real and an optional type $type (used for writing dates, since they're essentially the same), and returns the index into the offset table of the offset object that points to its location in the binary file. The bytes of the float are packed in reverse.

write_date $date

Takes a date $date and returns the index into the offset table of the offset object that points to its location in the binary file. Passes the date off to "write_real" for actual writing, since they're processed in the same manner, simply with different types.

write_null $null

Takes a null $null and passes it to "write_misc", along with an integer indicating what type of misc it is. The null belongs to the misc category (see "write_misc").

write_false $false

Takes a false $false and passes it to "write_misc", along with an integer indicating what type of misc it is. The false belongs to the misc category (see "write_misc").

write_true $true

Takes a true $true and passes it to "write_misc", along with an integer indicating what type of misc it is. The true belongs to the misc category (see "write_misc").

write_fill $fill

Takes a fill $fill and passes it to "write_misc", along with an integer indicating what type of misc it is. The fill belongs to the misc category (see "write_misc").

write_misc $type

Takes an integer indicating an object belonging to the misc category $type (false, null, true or fill) and returns the index into the offset table of the offset object that points to its location in the file.

Miscs are a group of data types not easily represented in Perl, and they are written with the only header byte containing a 0 to indicate that they are a misc and their misc type.

write_data $data

Takes some binary data $data and returns the index into the offset table of the offset object that points to its location in the file. Doesn't attempt to process the data at all.

count $data

Recursively counts the number of objects in a serialized data structure $data. Does not take into account duplicates, so this number might be slightly higher than the number of objects that is indicated in the 32-byte trailer.

binary_write $obj

Does the actual writing to the binary file. Takes some binary data $obj and writes it to the filehandle. Also adds the location of the binary data to the offset table and returns the index into the offset table of the current object.

power $int

Calculates the number of bytes necessary to encode an integer $int. Returns a power of 2 indicating the number of bytes.

bytes $int

Calculates the number of bytes necessary to encode an integer $int. Returns the actual number of bytes.

pack_in $int

Takes either a power of 2 or a number of bytes $int and returns the format pack() needs for encoding.