Hi Mug,

2007/2/22, Mug <[EMAIL PROTECTED]>:
Hello List,

My question is if this work safe ?

Here I have the way to use 2 modules and shares their methods cross
along, because
I don't want to split out the common part as another package, since
there are class properties
inside. If I split out something, I have to rebuild the object again in
the new package.

I think you're talking about the composite pattern, i.e. when an
object is composed by another object. I can't see problems if you use
something like:

package PackA;

sub new {
   my ($class) = @_;

   my $self = {};

   bless $self, $class;

   my $self->{B} = PackB->new($self);

   return $self;
}

package PackB;

sub new {
   my ($class, $packA_ref) = @_;

   my $self = {
       A => $packA_ref;
   };

   bless $self, $class;

   return $self;
}

This way they know about each other. But I think it is better if the
user creates the PackB and  initialize PackA passing it as argument,
i.e.:

my $o = PackA->new(PackB->new($my_packb_options));

And then, inside PackA when you do_something(), you can pass the whole
packA class as reference to PackB object:

sub do_something {
   my ($self) = @_;
   my $data = $self->{B}->do_other_thing($self);
   ...
}

This way you avoid coupling between PackB and PackA. PackA is composed
by PackB, but PackB does not really needs PackA to exist. You can
safely subclass PackA to PackAA, for instance:

package PackAA;

use base qw(PackA);

sub do_specific_thing {
   my ($self) = @_;
   my $data = $self->{B}->do_other_thing($self); # see, $self is
PackAA instead.
   ...
}

HTH!

--
Igor Sutton Lopes <[EMAIL PROTECTED]>

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


Reply via email to