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::Object - Perl module for providing OO methods to perl data structures.

SYNOPSIS

  use Data::Object;

  #Simpsons Data Refrence
  my $ref = {
                name => "Homer Simpson",
                children => [
                                {
                                    name => "Bart Simpson",
                                    age => 10,
                                    gender => "Male"
                                },
                                {
                                    name => "Lisa Simpson",
                                    age => 8,
                                    gender => "Female"
                                },
                                {
                                    name => "Maggie Simpson",
                                    age => 1, #Dont Bark at me its not realy given.
                                    gender => "Female"
                                }
                            ],
                age => 38,
                job => "Safety Inspector",
                gender => "Male",
                wife => {
                            name => "Marge Simpson",
                            age => 36,
                            job => "Housewife",
                            gender => "Female"
                        }
            };

  #Wrap the given refrence and return a class for working with the data.
  my $homer = Data::Object->wrap($ref);

  print $homer->wife->name;             # "Marge Simpson"
  print $homer->children->count;        # 3

  my $marge = $home->wife;
  $marge->children($homer->children);    # Homers kids are now Marges kids
  $marge->set("children",$home->get("children"));  #SAME
  #Base Data Structure is left in tact, and modified as one would think.
  print $homer->wife->children->count;  # 3

  #Traverse easily through an array of hashes
  print $homer->children->first( sub { $_->name eq "Lisa Simpson" } )->age;    # 8

  #Get Homers Daughters
  my @homers_girls = $homer->children->grep( sub { $_->gender eq "Female" } );
  foreach (@homers_girls) {
      print "Name: ",$_->name," Age: ",$_->age,"\n";
  }

DESCRIPTION

This module attempts to provide class style accessors for perl data structures. In an attempt to make it easier to traverse said data structures in an OO way.

Currently only Array and Hash structures are handled in any way, all others are returned as is.

See: Data::Object::Base, Data::Object::Hash, Data::Object::Array

METHODS

wrap

Wrap a data structure so that accessing it is possible via OO style methods.

TODO

Need to clean up the documentation, need to modify any existing methods that takes an annonymous sub and make it use D:O blessed objects.

Need more data manipulation methods. Like Sort for hash tables, sort_by_key(key) etc.

Overload cmp and <=> so that Data::Object instances can be compaired to each other. (by raw ref id)

AUTHOR

Jason Fried <fried@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2011 by Jason Fried. All rights reserved.

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