Module loading order

N

newtan

Hello,

Description:

package A;
my $instance;
sub new {
my $class = shift;
my %args = @_;
return $instance if $instance;
. . . stuff appropriate args into $instance ...
return bless $instance, $class;
}
.. . .

package B;
use A;
our @ISA = qw(A);
__PACKAGE_->SUPER::new(some args);
.. . .


package XYZ;
use A;
my $o = new A;
.. . .


Now using them


1 - This will work
#!/usr/bin/perl -w
use strict;
use B;
use XYZ;
.....


2 - This won't work
#!/usr/bin/perl -w
use strict;
use XYZ;
use B; <= this won't work
.....


Since in XYZ the singleton $o is actually instantiated with appropriate
parameters by the line __PACKAGE__->SUPER::new in B, the order of
module loading in 1) will work while that in 2) won't as 'use' implies
BEGIN blocks in FIFO.


Question: is there a way to ensure that a certain module is always
loaded before other modules no matter where it is with "use". In this
case, how can B be assured to be loaded before other aforementioned
modules? Thanks.
 
T

Todd W

Hello,

Description:

package A;
my $instance;
sub new {
my $class = shift;
my %args = @_;
return $instance if $instance;
. . . stuff appropriate args into $instance ...
return bless $instance, $class;
}
. . .

package B;
use A;
our @ISA = qw(A);
__PACKAGE_->SUPER::new(some args);
. . .


package XYZ;
use A;
my $o = new A;
. . .


Now using them


1 - This will work
#!/usr/bin/perl -w
use strict;
use B;
use XYZ;
....


2 - This won't work
#!/usr/bin/perl -w
use strict;
use XYZ;
use B; <= this won't work
....


Since in XYZ the singleton $o is actually instantiated with appropriate
parameters by the line __PACKAGE__->SUPER::new in B, the order of
module loading in 1) will work while that in 2) won't as 'use' implies
BEGIN blocks in FIFO.


Question: is there a way to ensure that a certain module is always
loaded before other modules no matter where it is with "use". In this
case, how can B be assured to be loaded before other aforementioned
modules? Thanks.

Instead of use()ing A in XYZ, "use B;" Then you won't need to say "use B;"
in your driver.

Todd W.
 
N

newtan

Todd said:
Instead of use()ing A in XYZ, "use B;" Then you won't need to say "use B;"
in your driver.

No, that would defeat the purpose of having the polymorphic behavior. A
is the parent of all subsequent children inherited from it, thus it's
able to act appropriately upon the type of the child package. B can't
be used in this case because there would be potentially more inherited
classes such as C, D, E etc..so hard-wired B there wouldn't work.
 
N

newtan

Ah, sorry that i didn't make it clearer. The intention of A being used
as a base class in XYZ is polymorphic. Depending on what type of child
class (such as B, C etc..), it will act appropriately. However, it
order for it to be somewhat useful, its child classes' constructor
(__PACKAGE_->SUPER::new) should be invoked first so that the object is
instantiated with appropriate attributes. Of course, the base class A
does provide some defaults, but in this case, it does need info of the
inherited classes. I do not want to restrict users to always place
"use ChildOfA" before other use's as that would make it unintuitive to
other programmers. Hope that's clearer and thanks for the suggestions.
 
X

xhoster

Hello,

Description:

package A;
my $instance;
sub new {
my $class = shift;
my %args = @_;
return $instance if $instance;
. . . stuff appropriate args into $instance ...

## Don't say it in English with ". . ." when you can say it in Perl.

$instance={%args};

## It is shorter, clearer, and allows us to run the code.

return bless $instance, $class;
}
. . .
1;


package B;
use A;
our @ISA = qw(A);
__PACKAGE_->SUPER::new(some args);

You are missing an "_" from __PACKAGE__.
There is already a module named B. Choose something
else for your example.

. . .

package XYZ;
use A;
my $o = new A;
. . .

Now using them

1 - This will work
#!/usr/bin/perl -w
use strict;
use B;
use XYZ;
....

2 - This won't work
#!/usr/bin/perl -w
use strict;
use XYZ;
use B; <= this won't work

It does "work", it just doesn't read your mind.

....

Since in XYZ the singleton $o is actually instantiated with appropriate
parameters by the line __PACKAGE__->SUPER::new in B, the order of
module loading in 1) will work while that in 2) won't as 'use' implies
BEGIN blocks in FIFO.

Question: is there a way to ensure that a certain module is always
loaded before other modules no matter where it is with "use".

I'm sure there is, but I doubt it is a very good way. perls needs to know
which module you wanted loaded first--it can't just read your mind. I'm
sure you could come up with some ackward way of telling it that using
source filters or such, but why not use "use" to tell it this?

Xho
 
X

xhoster

No, that would defeat the purpose of having the polymorphic behavior.

How can you have polymorphic behavior without having any "poly" to morph?
Your example only has one object. By design, apparently.
A
is the parent of all subsequent children inherited from it, thus it's
able to act appropriately upon the type of the child package. B can't
be used in this case because there would be potentially more inherited
classes such as C, D, E etc.

What is *supposed* to happen when you have these classes?

Xho
 
C

Ch Lamprecht

Hello,

Description:

package A;
my $instance;
sub new {
my $class = shift;
my %args = @_;
return $instance if $instance;
. . . stuff appropriate args into $instance ...
return bless $instance, $class;
}
.. . .

Hi,

are you aware of the fact, that this method will return the very same
blessed reference each time you call it (assuming that you assign a
reference to $instance in the part of your code you didn't show - before
calling bless).
You will have one 'instance' ...
This 'instance' will be shared by all your classes objects.
If your derived classes call this new-method for instantiation they will
share the same 'instance' too.
Is that what you want?
It does not look very much OO ...??

Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top