Data::AsObject(3)
π Data::AsObject π
okay bestie, sit down, hydrate, because we're about to talk about Data::AsObject β easy OO access to complex perl data structures. that's the whole vibe. no cap.
version: 0.07 β¨ (yes we're on the version, keep up)
πΈ 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->list # @array is ("Larry Wall", "Tom Christiansen", "Jon Orwant")
$book->{publisher} = "O'Reilly";
print $book->publisher # prints "O'Reilly"
likeβ¦ look at that. dots instead of arrows. the way that just works. π
π¦ DESCRIPTION π¦
Data::AsObject gives you easy object-oriented access to complex and arbitrarily nested perl data structures. it's literally built for working with hash-based representations of XML data, the kind generated by modules like XML::Complie or XML::TreePP.
π¨ WARNING π¨
bestie. listen. version0.06ofData::AsObjectbroke backward compatibility with two changes that may break your existing scripts. i'm not being dramatic, this is real.
- Automatic dereferencing in list context is no longer provided. use the
listmethod instead, periodt. - An attempt to access an non-existing hash key by default now dies rather than simply produce a warning. either explicitly request Data::AsObject not to die on missing hash keys, or use an exception handling mechanism to check if the data you want to access is actually there. π
π 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::AsObjectdoes not preform any autovivification, it protects you from misspelling a hash key when accessing its value (but see alsoHash::Utilfor 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::AsObjectcan automatically access such keys by substituting underscores for the non-standard symbols. iconic. - Easy dereferencing of arrayrefs β if you have a lot of arrayrefs in your data structure that often need to be traversed, e.g. with
grep,maporforeach,Data::AsObjectprovides alistmethod on arrayrefs to automatically dereference them.
β¨ 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.
Data::AsObject uses Sub::Exporter and allows you to import the dao sub in one of three modes:
π strict mode
use Data::AsObject dao => { mode => 'strict' };
in this mode (which is the default) dao will produce an object that dies whenever you try to invoke a hash key that does not exist. main character energy, honestly.
π loose mode
use Data::AsObject dao => { mode => 'loose' };
in this mode dao will produce an object that returns undef and issues a warning whenever you try to invoke a hash key that does not exist.
π§ strict mode
use Data::AsObject dao => { mode => 'silent' };
in this mode dao will produce an object that returns undef whenever you try to invoke a hash key that does not exist, but does not complain. (yes, the heading says strict mode again, that's what the original says, i don't make the rules π)
π 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
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
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
arrayrefs have a dereferencing list method. for example:
my $data = dao {
spain => [
{ name => 'spanish', numbers => ["uno", "dos", "tres", "cuatro"] },
{ name => 'catalan', numbers => ["un", "dos", "tres", "quatre"] },
];
};
foreach my $n ( $data->spain->list ) {
print $n->name . " ";
} # spanish catalan
π 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
π Autovivification
no autovivification is performed by default (but see FUNCTIONS above). an attempt to access a hash or array element that does not exist will produce a fatal error. use an exception handling mechanism such as Try::Tiny.
use Try::Tiny;
my $data = dao {
uk => ["one", "two", "three", "four"],
spain => ["uno", "dos", "tres", "cuatro"],
germany => ["eins", "zwei", "drei", "vier"].
};
try {
my $numbers = $data->bulgaria;
} catch {
warn "No info about Bulgaria!";
};
see also can below. β¬οΈ
π₯ Data::AsObject::Hash and special methods
if $data isa Data::AsObject::Hash:
canβ$data->canwill return the value of the$data->{can}element.$data->can("some_hash_key")will properly returnundefifsome_hash_keydoes not exists, or a reference to a sub that returns$data->{some_hash_key}otherwise.
my $data = dao {
uk => ["one", "two", "three", "four"],
# ...
};
warn "No info about Bulgaria!" unless $data->can('bulgaria');
VERSIONβ calling$data->VERSIONwill attempt to return the value of a hash element with a keyVERSION. useData::AsObject->VERSIONinstead.- others special methods β all other special methods and functions (
isa,ref,DESTROY) should behave as expected.
π BUGS π
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. we love a feedback loop π«Ά
β SEE ALSO β
Hash::AsObject
π AUTHOR π
Peter Shangov <pshangov@yahoo.com>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2011 by Peter Shangov.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page bestie!! brought to you by perl v5.42.0, dated 2025-07-25, filed under Data::AsObject(3) π go forth and dereference responsibly β¨
π¦ where this page comes from
- fedora 43: perl-Data-AsObject 0.07-43.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms