The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Data::Sofu - Perl extension for Sofu data

Synopsis

use Data::Sofu;
%hash=readSofu("file.sofu");
...
writeSofu("file.sofu",\%hash);

Or a litte more complex: use Data::Sofu qw/packSofu unpackSofu/; %hash=readSofu("file.sofu"); $comments=getSofucomments; open fh,">file.sofu"; writeSofu(\*fh,\$hash,$comments); close fh; $texta=packSofu($arrayref); $texth=packSofu($hashref); $arrayref=unpackSofu($texta); $arrayhash=unpackSofu($texth);

Synopsis - oo-style

require Data::Sofu;
my $sofu=new Sofu;
%hash=$sofu->read("file.sofu");
$comments=$sofu->comments;
$sofu->write("file.sofu",$hashref);
open fh,">file.sofu";
$sofu->write(\*fh,$hashref,$comments);
close fh;
$texta=$sofu->pack($arrayref);
$texth=$sofu->pack($hashref);
$arrayref=$sofu->unpack($texta);
$arrayhash=$sofu->unpack($texth);

DESCRIPTION

This Module provides the ability to read and write sofu files of the versions 0.1 and 0.2. Visit http://sofu.sf.net for a description about sofu.

It can also read not-so-wellformed sofu files and correct their errors.

Additionally it provides the ability to pack HASHes and ARRAYs to sofu strings and unpack those.

The comments in a sofu file can be preserved if they're saved with $sofu->comment or getSofucomments;

SYNTAX

This module can either be called using object-orientated notation or using the funtional interface. Some features are only avaiable when using OO.

FUNCTIONS

getSofucomments

Gets the comments of the last file read

writeSofu(FILE,DATA,[COMMENTS])

Writes a sofu file with the name FILE. FILE can be: A reference to a filehandle or a filename

An existing file of this name will be overwritten.

DATA can be a scalar, a hashref or an arrayref.

The top element of sofu files must be a hash, so any other datatype is converted to {Value=>DATA}.

@a=(1,2,3);
$sofu->write("Test.sofu",\@a);
%data=$sofu->read("Test.sofu");
@a=@{$data->{Value}}; # (1,2,3)

COMMENTS is s reference to hash with comments like the one retuned by comments()

readSofu(FILE)

Reads the sofu file FILE and returns a hash with the data. FILE can be: A reference to a filehandle or a filename

These methods are not exported by default:

packSofu(DATA)

Packs DATA to a sofu string. DATA can be a scalar, a hashref or an arrayref.

unpackSofu(SOFU STRING)

This function unpacks SOFU STRING and returns a scalar, which can be either a string or a reference to a hash or a reference to an array.

METHODS (OO)

new

Creates a new Data::Sofu object.

setIndent(INDENT)

Sets the indent to INDENT. Default indent is "\t".

setWarnings( 1/0 )

Enables/Disables sofu syntax warnings.

comments

Gets/sets the comments of the last file read

write(FILE,DATA,[COMMENTS])

Writes a sofu file with the name FILE. FILE can be: A reference to a filehandle or a filename

An existing file of this name will be overwritten.

DATA can be a scalar, a hashref or an arrayref.

The top element of sofu files must be a hash, so any other datatype is converted to {Value=>DATA}.

@a=(1,2,3);
$sofu->write("Test.sofu",\@a);
%data=$sofu->read("Test.sofu");
@a=@{$data->{Value}}; # (1,2,3)

COMMENTS is s reference to hash with comments like the one retuned by comments()

read(FILE)

Reads the sofu file FILE and returns a hash with the data. FILE can be: A reference to a filehandle or a filename

pack(DATA)

Packs DATA to a sofu string. DATA can be a scalar, a hashref or an arrayref.

unpack(SOFU STRING)

This function unpacks SOFU STRING and returns a scalar, which can be either a string or a reference to a hash or a reference to an array.

BUGS

Hashes with keys other than strings without whitespaces are not supported due to the restrictions of the sofu file format.

Crossreference will trigger a warning.

Comments written after an object will be rewritten at the top of an object:

foo = { # Comment1
	Bar = "Baz"
} # Comment2

will get to:

foo = { # Comment1
# Comment 2
	Bar = "Baz"
} 

SEE ALSO

perl(1),http://sofu.sf.net