On 11/04/2006 06:05 AM, Muttley Meen wrote:
On 11/3/06, Mumia W. <[EMAIL PROTECTED]> wrote:

Please bottom-post.

Why should a call to Create() on an object returned by Package2()
update the object $a ?

It shouldn't normally update the containing object, and it doesn't in
your program.

For your education, try this,
print "Inside ERR:\t", $a->Package2->err, "\n";

Also, typically one uses either containment or inheritance but not both.


What I meant was, "in a single class (A), usually a programmer will either inherit from a base class (B) or contain an object of that class (B), but not both inherit from (B) and contain (B)."


Please explain what you're trying to do again.

The ideea is to implement a context based protocol, though you can't
see that from the script.
Let's say that you have several contexts, like
Context1
   ->Context2/OP
       ->Context2.1/OP
   ->Context3/OP
Given this, I wanted to reflect the context structure into the code written by
the module user, something like
Context1->Context2->OP ( Where OP is any givent operation).
Now, the operations might trigger protocol errors, which are
held(counted) in the parent class( Context1 ), and thus should be
modifiable by any child method( in this example,
by Context2/OP, Context2.1/OP, Context3/OP).

getting back the the example script, Package1 would be the base class, meaning
in the same time the top context.
Package2, would be in the same time a method of Package1, in order to
call it like
Package1->Package2->OP, and it would be a package of itself too.

The IncErr method simply (would)increment the number of errors reported
by the protocol.


I suggest you let all of the contexts use the same base class and let all of the objects share the same error count variable (using references), e.g.

#!/usr/bin/perl
use strict;
use warnings;
use Class::Struct;
use Data::Dumper;

struct ContextBase => { errRef => '$', context => '$' };

package ContextBase;

sub addContext {
    no strict 'refs';
    my $self = shift;
    my $context = shift;    # context package name

    # Check the context package name.
    die "Bad context" unless ($context =~ /^Context\w/);
    my $newctx = $context->new;

    # This object now contains a new context object.
    $self->context($newctx);

    # The new context object should know about the error
    # count variable.
    $newctx->errRef($self->errRef);

    # Create a method for accessing the sub-context.
    *{$context} = sub {
        my $self = shift;
        $self->context(shift()) if (@_);
        $self->context;
    };
    $newctx;
}

sub err {
    ${shift()->errRef};
}

sub incErr {
    ++${shift()->errRef}
}

package Context1;
our @ISA = qw(ContextBase);

package Context2;
our @ISA = qw(ContextBase);

package Context3;
our @ISA = qw(ContextBase);

######## Main program ###########
package main;

my $errCount = 0;
my $obj = Context1->new(errRef => \$errCount);
$obj->addContext('Context2')->addContext('Context3');
print Dumper(\$obj);

print '----------------------', "\n";
$obj->incErr;
print 'Error Count: ', $obj->err(), "\n";
$obj->Context2->incErr;
print 'Error Count: ', $obj->err(), "\n";
$obj->Context2->Context3->incErr;
print 'Error Count: ', $obj->err(), "\n";

__END__


If you don't have the Class::Struct module, get it. It simplifies
creating classes that have many accessor methods. Also get
Class::Accessor and Class::Data::Inheritable; they do the same thing as
Class::Struct but are more powerful.


HTH




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to