Home > Archive > PERL Beginners > February 2007 > 2 modules share each other's methods, is that safe ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
2 modules share each other's methods, is that safe ?
|
|
|
| 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.
In Main, I call PackA to do something, which Pack A need PackB to do
something, however,
PackB need another method in PackA to do something also. Because the
{OuterClass} is
actually the exact reference in PackA, so I think that's okay, I think.
In real case, PackA is the user interface on drawing calendar ( daily,
w ly, monthly, yearly ),
where PackB is a Format feeder. The way I need PackB to call PackA again
is that I want to call
the *Monlty method 12 times so forming the *Yearly one.
Thanks for advise,
Mug
########################################
#######################
package PackB ;
sub Do_Something_Else {
# do something and return something
}
sub Do_Something {
my $class = shift;
my $data = $class -> {OuterClass} -> Func_In_A2 ;
$class -> Do_Something_Else ( $data ) ;
# do something and return something
}
sub Construct {
my $self = {};
$self -> {OuterClass} = shift;
return $self;
}
1;
__END__
########################################
#########################
package PackA;
use PackB;
sub Func_In_A2 {
# do something and return something
}
sub Func_In_A {
# do something and return something
}
sub MainFunc {
my $class = shift;
my $B_Func = PackB::Construct ;
return $B_Func -> Do_Something
}
sub Construct_A {
bless {};
}
1;
__END__
########################################
#########################
# MAIN
use PackA;
my $O = Constuct_A ;
print $O -> MainFunc ( "Argv" ) ;
| |
| Igor Sutton 2007-02-22, 4:00 am |
| Hi Mug,
2007/2/22, Mug <perl@reborn.org>:
> 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 <igor.sutton@gmail.com>
| |
|
| Hi and thanks Sutton,
> Igor Sutton wrote:
> Hi Mug,
>
> 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;
> }
That's a very great approach to make them know each other as early as
by the construct level ! I don't need to worry about where I should create
there link-up spot then.. I have made it through now ! thanks!
>
> 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.
> ...
> }
>
That's even a more interesting way to split the package, but unfortunate
that
I can only try it next time. Yet already wrote too many uneasy bulky =(
Thanks again !
Mug
|
|
|
|
|