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

Object::KVC::Hash - Key Value Coding Hash object

SYNOPSIS

  use Object::KVC::Hash;
  use Object::KVC::String;
  
  my $object1 = Object::KVC::Hash->new();
  my $object2 = Object::KVC::Hash->new();
  
  $object1->set( "id", Object::KVC::String->new("id1234") );
  $object2->set( "id", Object::KVC::String->new("id1234") );

  print $object1->get( "id" )->as_string();

  $object1->equals( $object2 ) ? print "Yes, the objects are equal";

DESCRIPTION

Object::KVC::Hash is a generic object which can be used to model a variety of objects without having to write a large number of classes.

Values must be wrapped in an object supporting the "equals," "contains," "intersects," and "as_string" methods.

The "equals," "contains," and "intersects," methods allow two Object::KVC::Hash object properties to be compared.

The "equals," "contains," and "intersects" methods allow searching of Object::KVC::Hash objects within an Object::KVC::List.

METHODS

new()

The constructor.

   my $object = Object::KVC::Hash->new();

No arguments.

clone()

Returns a new Object::KVC::Hash object with the same key value pairs as the original object.

  my $cloned_object = $object->clone(); 

Does not copy the value objects.

set( <string>, <value object> )

Set a key value pair.

  $object->set( "key",  Object::KVC::String->new("string") );

Set throws an exception if the value object does not support the required methods.

Same as:

  $object->{ "key" } = Object::KVC::String->new("string");

Without type checking.

get( <string> )

Get a value object.

  my $value = $object->get( "key" );

Throws an exception if the specified key if not defined.

Same as:

  my $value = $object->{"key"};

Without checking that the key is defined.

equals( $other<Object::KVC::Hash> )

Returns true if all keys exist in both objects and the corresponding value objects are equal.

  $object1->equals( $object2 );
  

matches( $other<Object::KVC::Hash> )

Returns true if all keys in $object2 exist in $object1 and the corresponding value objects are equal.

Returns false if $object2 has a key which does not exist in $object1.

  $object1->matches( $object2 );

intersects( $other<Object::KVC::Hash> )

Returns true if all keys in $object2 exist in $object1 and the corresponding value objects intersect.

Returns false if $object2 has a key which does not exist in $object1.

  $object1->intersects( $object2 );

contains( $other<Object::KVC::Hash> )

Returns true if all keys in $object2 exist in $object1 and the corresponding value objects in $object1 contain the corresponding value objects in $object2.

Returns false if $object2 has a key which does not exist in $object1.

  $object1->contains( $object2 );

contained_by( $other<Object::KVC::Hash> )

Returns true if all keys in $object2 exist in $object1 and the corresponding value objects in $object2 contain the corresponding value objects in $object1.

Returns false if $object2 has a key which does not exist in $object1.

  $object1->contained_by( $object2 );

get_keys()

Returns 'ARRAY' of currently defined keys.

  my @keys = $object->get_keys()

Same as:

  my @keys = keys( %$object );

has_defined( <string> )

Returns true if a key value pair is defined.

  $object->has_defined( "key" );

Same as:

  defined( $object->{"key"} );

delete_key( <string> )

Delete a key value pair.

  $object->delete_key( "key" );

Same as.

  delete( $object->{"key"} );

as_string()

For debugging only.

dump()

For debugging only.

  print $object->dump();

COPYRIGHT AND LICENSE

Object::KVC::Hash Copyright (C) 2012 Trystan Johnson

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.