In a nutshell, I'm trying to simulate a network topology using objects for each instance on the network. In addition, I got the top-level manager class responsible for managing these objects and performing integrity checks. The file structure is as follows (I left most of the object files since they are all structured pretty much the same):
Manager.pm Constants.pm Classes/ +- Machine.pm +- Node.pm +- Object.pm +- Switch.pm
Since quite a few years in OOP, I have been a fan of code reuse, etc., so I set inheritance between these objects, the inheritance tree (in this example) looks like this:
Switch -+-> Node -+-> Object Machine -+
All of these objects are structured as follows:
package Switch; use parent qw(Node); sub buildFromXML { ... } sub new { ... }
Now for the interesting part:
Question 1
How can I ensure that all of these objects load correctly without typing names statically? The main problem is this: if I just require "$_" foreach glob("./Classes/*"); , I get a lot of "Subroutine with new overridden errors" errors. I also played with use parent qw(-norequire Object) , Module::Find and some other modifications of @INC in various combinations to make it short: it did not work. Currently, I am statically importing all the classes used, they automatically import the parent classes.
So basically, what I'm asking is: what is the (perl-) right way to do this?
And advanced: it would be very useful to create a more complex folder structure (since there will be quite a lot of objects) and still have inheritance + "autoload"
Question 2 - SOLVED
How can I “share my import”? I use several libraries (my own, containing some helper functions, LibXML , Scalar::Util , etc.), and I want to share them between my objects. (The reason for this is that I may need to add another shared library to all objects, and there is a possibility that there will be much more than 100 objects - without fun editing all of them manually, and this is theoretically with a regex / script, but this is not seems like the cleanest solution)
What I tried:
- import everything into
Manager.pm Works inside the Manager package - gives me errors like "undefined routine & Switch :: trace" - Create an
include.pl file and do / require / use inside each object - gives me the same errors. - A few more things, which I, unfortunately, do not remember.
include.pl will basically look like this:
use lib_perl; use Scalar::Util qw(blessed); use XML::LibXML; use Data::Dumper; use Error::TryCatch; ...
Again I ask: what is the right way to do this? Am I using the right approach and just not doing the execution or should I completely restructure?
It doesn't matter why my current code doesn’t work so well, providing the correct, clean approach for these problems will be enough :)
EDIT: a completely forgotten version of perl -_- Sidenote: I cannot update perl since I need libraries stuck with 5.8: /
C:\> perl -version This is perl, v5.8.8 built for MSWin32-x86-multi-thread (with 50 registered patches, see perl -V for more detail) Copyright 1987-2006, Larry Wall Binary build 820 [274739] provided by ActiveState http://www.ActiveState.com Built Jan 23 2007 15:57:46