NAME

Data::AsObject - Easy OO access to complex perl data structures

VERSION

Version 0.04

SYNOPSIS

	use Data::AsObject qw(dao);
	
	my $book = dao { 
		name      => "Programming Perl",
		authors   => ["Larry Wall", "Tom Christiansen", "Jon Orwant"],
		
	};
    
	print $book->name              # prints "Programming Perl"
	print $book->authors(0)        # prints "Larry Wall"
	my $array_ref = $book->authors # $array_ref is ["Larry Wall", "Tom Christiansen", "Jon Orwant"]
	my @array = $book->authors     # @array is ("Larry Wall", "Tom Christiansen", "Jon Orwant")
	$book->{publisher} = "O'Reilly";
	print $book->publisher         # prints "O'Reilly"

DESCRIPTION

Data::AsObject provides easy object-oriented access to complex and arbitrarily nested perl data structures. It is particulary suitable for working with hash-based representation of XML data, as generated by modules like XML::Complie or XML::TreePP.

BENEFITS

These are some of the reasons why you may want to use Data::AsObject:

Object-oriented syntax

The object-oriented syntax may sometimes be more appropriate than the traditional hashref and arrayref syntax.

Protection from misspelled hash key names

Since Data::AsObject does not preform any autovivification, it protects you from misspelling a hash key when accessing its value (but see also Hash::Util for more robust ways to do that).

Easy access to hash keys with non-standard symbols

If your hashes contain a lot of keys with dashes or colons, as is often the case with keys representing xml element names, Data::AsObject can autmatically access such keys by substituting underscores for the non-standard symbols.

FUNCTIONS

dao

Takes as input one or more hash or array references, and returns one or more objects (Data::AsObject::Hash or Data::AsObject::Array respectively) that can be used to access the data structures via an object oriented interface. Exported by default.

USAGE

Working with hashes

To access hash elements by key, use the hash key as method name:

my $data = dao { three => { two => { one => "kaboom" } } };
print $data->three->two->one; # kaboom

In scalar context, a hash reference is returned as a reference, and in list context, as a list:

my $data = dao { 
	one => { en => "one", de => "ein", es => "uno" }, 
	two => { en => "two", de => "zwei", es => "dos" },
};

my $hasref = $data->one; # { en => "one", de => "ein", es => "uno" }, 
my %hash = $data->two; # ( en => "two", de => "zwei", es => "dos" ),

So if you want to pass a hashref as an argument to a subroutine, you will have to put some extra effort and do one of the following:

foo( {$data->one} );

or

my $hashref = $data->one;
foo($hashref);

Alternatively you ca do:

foo( $data->{one} );

but you get no guarantee whether this will fetch you a plain hashref or a Data::AsObject::Hash object.

If a hash key contains one or more colons or dashes, you can access its value by substituting underscores for the colons or dashes (the underlying hash key name is not modified).

my $data = dao { 
	'xml:lang'     => "EN", 
	'element-name' => "some name",
};

print $data->xml_lang     # "EN"
print $data->element_name # "some name"

Working with arrays

To access array items pass the item index as an argument to the hash that contains the array:

my $data = dao {
	uk => ["one", "two", "three", "four"],
	spain => [ 
		{ name => 'spanish', numbers => ["uno", "dos", "tres", "cuatro"] },
		{ name => 'catalan', numbers => ["un", "dos", "tres", "quatre"] },
	];
};

print $data->en(1) # two
print $data->spain(0)->numbers(3); # cuatro

In scalar context, an array reference is returned as a reference, and in list context, as a list:

foreach my $n ( $data->spain(1)->numbers ) {
	print $n . " ";
} # un dos tres quatre

Array of array structures are a little bit clumsier to work with. You will need to use the get method of Data::AsObject::Array and pass it the index of the item you want to access:

my $data = dao [
	["one", "two", "three", "four"]
	["uno", "dos", "tres", "cuatro"],
	["un", "dos", "tres", "quatre"],
];

print $data->get(2)->get(0); # un

If your base reference is an arrayref, you can fetch all its elements in list context via the all method:

my $data = dao [1, 2, 3];
my @array = $data->all # same as: my @array = @$data

Modifying data

Data::AsObject only provides accessor functions. To modify data, access the respective hash or array element directly:

my $data = dao {};
$data->{one} = "uno";
print $data->one # uno

Note that the accessor methods return references to the underlying data structure rather than clones:

my $data = dao {};
my $copy = $data;

$data->{one} = "uno";
print $copy->one # uno

Autovivification

No autovivification is performed. An attempt to access a hash or array element that does not exist will issue a warning and return undef.

Data::AsObject::Hash and special methods

If $data isa Data::AsObject::Hash:

can

Attempts to call $data->can("some_method_name") will always return undef, regardless of whether a $data->{"some_method_name"} hash key exists or not.

VERSION

Calling $data->VERSION will attempt to return the value of a hash element with a key "VERSION". Use Data::AsObject->VERSION instead.

others special methods

All other special methods and functions (isa, ref, DESTROY) should behave as expected.

AUTHOR

Petar Shangov, <pshangov at yahoo dot com>

BUGS

This is still considered alpha-stage software, so problems are expected. Please report any bugs or feature requests to bug-data-object at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Object. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Data::Object

You can also look for information at:

SEE ALSO

Hash::AsObject

COPYRIGHT & LICENSE

Copyright 2009 Petar Shangov, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.