PogoLink - Bidirectional relationship class for objects in a Pogo database
use PogoLink; # Define relationships package Person; sub new { my($class, $name) = @_; my $self = new_tie Pogo::Hash 8, undef, $class; %$self = ( NAME => $name, FATHER => new PogoLink::Scalar($self, 'Man', 'CHILDREN'), MOTHER => new PogoLink::Scalar($self, 'Woman', 'CHILDREN'), FRIENDS => new PogoLink::Btree ($self, 'Person', 'FRIENDS', 'NAME'), ); $self; } package Man; @ISA = qw(Person); sub new { my($class, $name) = @_; my $self = $class->SUPER::new($name); $self->{CHILDREN} = new PogoLink::Array ($self, 'Person', 'FATHER'); $self->{WIFE} = new PogoLink::Scalar($self, 'Woman', 'HUS'); $self; } package Woman; @ISA = qw(Person); sub new { my($class, $name) = @_; my $self = $class->SUPER::new($name); $self->{CHILDREN} = new PogoLink::Array ($self, 'Person', 'MOTHER'); $self->{HUS} = new PogoLink::Scalar($self, 'Man', 'WIFE'); $self; } # Use relationships $Dad = new Man 'Dad'; $Mom = new Woman 'Mom'; $Jr = new Man 'Jr'; $Gal = new Woman 'Gal'; # Marriage $Dad->{WIFE}->add($Mom); # $Mom->{HUS} links to $Dad automatically # Birth $Dad->{CHILDREN}->add($Jr); # $Jr->{FATHER} links to $Dad automatically $Mom->{CHILDREN}->add($Jr); # $Jr->{MOTHER} links to $Mom automatically # Jr gets friend $Jr->{FRIENDS}->add($Gal); # $Gal->{FRIENDS} links to $Jr automatically # Oops! Gal gets Dad $Gal->{HUS}->add($Dad); # $Dad->{WIFE} links to $Gal automatically # $Mom->{HUS} unlinks to $Dad automatically
PogoLink makes single-single or single-multi or multi-multi bidirectional relationships between objects in a Pogo database. The relationships are automatically maintained to link each other correctly. You can choose one of Pogo::Array, Pogo::Hash, Pogo::Htree, Pogo::Btree and Pogo::Ntree to make a multi end of link.
This class makes a single end of link.
This class makes a multi end of link as an array. It uses Pogo::Array to have links.
These classes make a multi end of link as a hash. Each uses corresponding Pogo::* to have links.
Constructor. Class method. $selfobject is a object in the database which possesses this link. It must be a object as a hash reference. $linkclass is a class name of linked object. If $linkclass defaults, any class object is allowed. $invfield is a field (i.e. hash key) name of the linked object which links inversely. $keyfield is only necessary for PogoLink::Hash, PogoLink::Htree, PogoLink::Btree, PogoLink::Ntree. It specifies a field name of the linked object thats value is used as the key of this link hash. $size may be specified for PogoLink::Array, PogoLink::Hash or PogoLink::Htree. $size will be used when internal linking Pogo::Array, Pogo::Hash or Pogo::Htree object will be constructed.
NOTE: You cannot use PogoLink::* constructors as follows in a class constructor.
sub new { my($class) = @_; my $self = {}; bless $self, $class; $self->{FOO} = new PogoLink::Scalar $self, 'Foo', 'BAR'; $self; }
Because the self-object which is passed to PogoLink::* constructors must be tied to a Pogo::* object. In the above sample, $self is a Perl object on the memory yet. The right way is as follows.
sub new { my($class) = @_; my $self = new_tie Pogo::Hash 8, undef, $class; $self->{FOO} = new PogoLink::Scalar $self, 'Foo', 'BAR'; $self; }
You can make a blessed reference which is tied to specified Pogo::* object by using new_tie which takes a class name as arguments.
Get the linked object. For PogoLink::Scalar, $idx_or_key is not necessary. For PogoLink::Array, $idx_or_key is an array index number. For other, $idx_or_key is a hash key string.
Get the linked object list.
Get the hash key list of linked objects. Only available for PogoLink::Hash, PogoLink::Htree, PogoLink::Btree, PogoLink::Ntree.
Test the link if it links to $object.
Unlink to all objects in the link.
Unlink to $object.
Link to $object. The inverse field (it's name was specified as $invfield by new()) of $object must be a PogoLink::* object. If the inverse field is not defined yet and $object has INIT_fieldname method (e.g. the field name is 'FIELD', the method name is 'INIT_FIELD'), this method calls $object->INIT_fieldname() to initialize the inverse field before linking.
Sey Nakajima <nakajima@netstock.co.jp>
Pogo(3). sample/person.pl.
4 POD Errors
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head2'
'=item' outside of any '=over'
To install Pogo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Pogo
CPAN shell
perl -MCPAN -e shell install Pogo
For more information on module installation, please visit the detailed CPAN module installation guide.