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

Puppet::Body - Utility class to handle permanent data, has-a relations and logs

SYNOPSIS

 use Puppet::Body ;

 package myClass ;
 sub new
  {
    my $type = shift ;
    my $self = {};
    $self->{body} = new Puppet::Body(cloth => $self, @_) ;
    bless $self,$type ;
  }

 sub body { return shift->{body} ;}

 package main ;

 # create a class with no persistent data
 my $foo = new myClass (Name => 'foo') ;

 # foo now has baz and buz
 $foo->body->acquire(body => $baz);
 $foo->body->acquire(body => $buz);

 # foo no longer has $baz
 $foo->body->drop($baz);

 # create a class with persistent data
 my $file = 'test.db';
 my %dbhash;
 # you manage the DB file
 tie %dbhash, 'MLDBM', $file , O_CREAT|O_RDWR, 0640 or die $! ;

 my $test = new myClass 
   (
    name => 'test',
    dbHash => \%dbhash,
    keyRoot => 'key root'
   );

 # store some data in permanent storage
 $test->storeDbInfo(toto => 'toto val', dummy => 'null') ;

 # remove some data from permanent storage
 $test->deleteDbInfo('dummy');

 # at the end of your program, just to be on the safe side
 untie %dbhash ;

DESCRIPTION

Puppet::Body is a utility class that is used (and not inherited like the deprecated Puppet::Any) to manage dynamic has-a relations between objects.

This class provides the following features :

  • An event log display so user object may log their activity (with Puppet::LogBody)

  • A Debug log display so user object may log their "accidental" activities(with Puppet::LogBody)

  • A set of functions to managed "has-a" relationship between Puppet objects.

  • A facility to store data on a database file tied to a hash.(with MLDBM)

Constructor

new(...)

Creates new Puppet object. New() parameters are:

name

The name of your object (defaults to "anonymous1" or "anonymous2" ...)

cloth

The ref of your object

keyRoot

See "Database management".

dbHash

ref of the tied hash. See "Database management".

how

Specify how logs are to be handled. See "Constructor" in Puppet::LogBody

Generic methods

getName()

Returns the name of this object.

HAS-A relations management methods

acquire(...)

Acquire the object ref as a child. Parameters are:

body

Reference of the Puppet::Body object that is to be acquired.

name

Name to refer to the acquired Puppet::Body. Defaults to the name of the acquired Puppet::Body object.

For instance if object foo acquires object bar, bar becomes part of foo's content and foo is one of the container of bar.

drop('name')

Does the opposite of acquire(), i.e. drop the object named 'name'.

getContent('name',...)

In scalar context, it returns the Puppet::Body ref of the object 'name'. Returns undef if 'name' is not part of the content (i.e if it has not been 'acquired' before)

In array context, it returns an array of all Puppet::Body references of all passed names. Returns () if the object has no content.

getContainer('name',...)

Same as getContent for the container.

contentNames()

Returns all names of the content, i.e. of all 'acquired' objects.

containerNames()

Returns all names of the containers, i.e. of all objects that 'acquired' this object

Log management.

printDebug(text)

Will log the passed text into the debug and events log object.

printEvent(text)

Will log the passed text into the events log object.

Database management

The class is designed to store its data in a database file. (For this you should use MLDBM if you want to store more than a scalar in the database). The key for this entry is "$keyRoot;$name# $key". keyRoot and name being passed to the constructor of the object and 'key' is the name of the value you want to store (This may remind perl old timers of a trick to emulate multi-dimensional hashes in perl4)

Needless to say, creating two instances of Puppet::Body with the same keyRoot and name is a bad idea because you may mix up data from one object to another.

storeDbInfo(%hash)

Store the passed hash in the database. You may also pass a hash ref as single argument.

deleteDbInfo(key,...)

delete the "key" entries from the database.

getDbInfo(key,...)

If one key is specified, getDbInfo will return the value of the "key" entry from the database.

If more than one key is passed, getDbInfo will return a hash ref containing a copy of the key,value pairs from the database.

About Puppet body classes

Puppet classes are a set of utility classes which can be used by any object. If you use directly the Puppet::Body class, you get the plain functionnality. And if you use the Puppet::Show class, you can get the same functionnality and a Tk Gui to manage it.

AUTHOR

Dominique Dumont, Dominique_Dumont@grenoble.hp.com

Copyright (c) 1998-1999 Dominique Dumont. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

perl(1), Puppet::Log(3), Puppet::Show(3)