Data::Sofu::Binary - Interface to various binary drivers
This module can be used to convert complex data structures and SofuObject trees to binary files and streams.
use Data::Sofu qw/readSofu writeSofuBinary/; my $tree = [%ENV]; $tree->{Foo}=@INC; writeSofuBinary("env.sofu",$tree); #Write as a binary sofu file my $tree2=readSofu("env.sofu"); #Reading doesn't care if its binary or normal sofu. #Or using just this module: my $tree = [%ENV]; $tree->{Foo}=@INC; require Data::Sofu::Binary; my $bsofu=Data::Sofu::Binary->new(); my $bstream = $bsofu->pack($tree); my $tree2=$bsofu->read(\$tree); # This can only read binary data. #More detailed: writeSofuBinary("env.sofu",$tree,$comments,$encoding,$byteorder,$mark); #For details on these parameters see the pack() Method.
This Module is pure OO, exports nothing
These are the known binary drivers (for now):
Data::Sofu::Binary::Bin0200
Sofu binary version 0.2.0.0 Driver.
Note
Data::Sofu's writeBinary will always take the latest stable one.
These Methods are also avaiable for the returned binary driver.
Also see the Data::Sofu::Binary::Bin0200 or whatever driver you are using for more methods.
Creates a new Binary Driver using DRIVER or the latest one available.
require Data::Sofu::Binary; $bsofu = Data::Sofu::Binary->new(); #Using the latest. $bsofu = Data::Sofu::Binary->new("000_002_000_000"); Taking a specific one. #You can call it directly: require C<Data::Sofu::Binary::Bin0200>; $bsofu = C<Data::Sofu::Binary::Bin0200>->new(); #The same
Internal method, will throw an exception containing a stacktrace and the offset of the file where it happened.
A helper method to open files
File can be:
A filename, (the file will be opened in raw mode)
a filehandle, (will be set to binmode)
or a scalarref (data will be written to/form the referenced scalar
Same as open() for output.
Internal method, used to read AMOUNT bytes from the filestream.
Internal method, determines endianess and version the binary file was written in.
Returns ByteOrderMark and Sofu Version.
Reads FILE in binary mode and returns a perl datastructure (Hashes, Arrays, Scalars)
See open() for info on the FILE parameter.
Loads automatically the right driver for FILE, no matter what driver is in use right now. But it will keep the current driver if it can read it.
Will not change the driver you are currently using!
Reads FILE in binary mode and returns a Sofu datastructure (Data::Sofu::Object's, Maps, Lists and Values)
write(FILE,TREE,[COMMENTS,[ENCODING,[BYTEORDER,[SOFUMARK,[...]]]]])
Writes TREE to FILE.
See open() for FILE.
See pack() for COMMENTS,ENCODING,BYTEORDER,SOFUMARK,...
TREE can be a perl datastructure or a Data::Sofu::Object or derived.
pack(TREE,[COMMENTS,[ENCODING,[BYTEORDER,[SOFUMARK,[...]]]]])
This method is implemented only in the driver, but it is important to discuss the arguments here.
Note: These arguments are the ones used in drivers up to the default Data::Sofu::Binary::Bin0200. Later drivers might add more arguments (therefore ...), and earlier drivers might support fewer.
print FH, $bsofu->pack(readSofu("something.sofu"),getSofucomments(),"UTF-32","LE","0.4");
First driver to support: Data::Sofu::Binary::Bin0200
Perl datastructure to pack. Can be a hash, array or scalar (or array of hashes of hashes of arrays or whatever). Anything NOT a hash will be converted to TREE={Value=>TREE};
It can also be a Data::Sofu::Object or derived (Data::Sofu::Map, Data::Sofu::List, Data::Sofu::Value, Data::Sofu::...). Anything not a Data::Sofu::Map will be converted to one (A Map with one attribute called "Value" that holds TREE).
Comment hash (as returned by Data::Sofu::getSofucomments() or Data::Sofu->new()->comments() after any file was read).
Can be undef or {}.
Specifies the encoding of the strings in the binary sofu file, which can be:
"0"
"UTF-8"
This is default.
Normal UTF-8 encoding (supports almost all chars)
"1"
"UTF-7"
This is default for byteorder = 7Bit (See below)
7Bit encoding (if your transport stream isn't 8-Bit safe
"2"
"UTF-16"
UTF 16 with byte order mark in EVERY string.
Byteoder depends on your machine
"3"
"UTF-16BE"
No BOM, always BigEndian
"4"
"UTF-16LE"
No BOM, always LittleEndian
"5"
"UTF-32"
UTF-32 with byte order mark in EVERY string.
"6"
"UTF-32BE"
"7"
"UTF-32LE"
"8","9"
Reserved for future use
"10"
"ascii"
Normal ASCII encoding
Might not support all characters and will warn about that.
"11"
"cp1252"
Windows Codepage 1252
"12"
"latin1"
ISO Latin 1
"13"
"latin9"
ISO Latin 9
"14"
"latin10"
ISO Latin 10
Defines how the integers of the binary file are encoded.
undef
Maschine order
This is Default.
BOM is placed to detect the order used.
"LE"
Little Endian
Use this to give it to machines which are using Little Endian and have to read the file alot
"BE"
Big Endian
Use this to give it to machines which are using Big Endian and have to read the file alot
"7Bit"
Use this byteorder if you can't trust your transport stream to be 8-Bit save.
Encoding is forced to be UTF-7. No byte in the file will be > 127.
BOM is set to 00 00.
"NOFORCE7Bit"
Use this byteorder if you can't trust your transport stream to be 8-Bit save but you want another enconding than UTF-7
Encoding is NOT forced to be UTF-7.
Defines how often the string "Sofu" is placed in the file (to tell any user with a text-editor what type of file this one is).
Only place one "Sofu" at the beginning of the file.
"0" or ""
Place no string anywhere.
"1" or >1
Place a string on every place it is possible
Warning, the file might get big.
"0.000001" - "0.99999"
Place strings randomly.
NOTE:
Encoding, Byteorder and encoding driver (and Sofumark of course) are saved in the binary file. So you don't need to specify them for reading files, in fact just give them the Data::Sofu's readSofu() and all will be fine.
Data::Sofu::Object->writeBinary() will only use the Bin0200 driver, no other.
Data::Sofu::Object->writeBinary()
$map = new Data::Sofu::Map; ..... $map->writeBinary($file); #Bin0200 driver always. use Data::Sofu; writeSofuBinary($file,$map); #Will use the latest driver.
perl(1),http://sofu.sf.net
Data::Sofu::Object, Data::Sofu, Data::Sofu::Binary::Bin0200
To install Data::Sofu, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Sofu
CPAN shell
perl -MCPAN -e shell install Data::Sofu
For more information on module installation, please visit the detailed CPAN module installation guide.