in reply to Re: How can I avoid creating two instances of the same object
in thread How can I avoid creating two instances of the same object

"I think you want to make an object whose @ISA includes all the relevant class names (whose methods hopefully do not clash). Or you can call the desired methods as class methods rather than through the object, passing the object as the first argument"

Yes! that is what I'm looking for. I thought about adding each module to the @ISA for what was the parent module already. If I do this, I will be adding close to 200 modules to the @ISA. Will this preload them each time it is loaded or will it load them as each method is called. With the other route, how do call the methods as class methods? Speed is of moderate concern. It doesn't have to be blazingly fast but it can't be slow either
  • Comment on Re^2: How can I avoid creating two instances of the same object

Replies are listed 'Best First'.
Re^3: How can I avoid creating two instances of the same object
by Roy Johnson (Monsignor) on Mar 02, 2006 at 18:01 UTC
    @ISA will not load the modules, you have to do that yourself. @ISA merely tells Perl what namespaces to look through to find methods. If the namespace is empty (because you haven't loaded a module), Perl doesn't particularly mind.

    The example I gave you showed you how you would call a method as a class method. The equivalent object-style call was in the comments. Speedwise, I think it's a slight benefit to call things as class methods, because Perl doesn't have to search the inheritance tree.

    Using your original example, your code might looks like this:

    use Data::Dumper; use User; my $dbh = User->new(); use Company; # Pretend that $dbh is a Company and call its do_something method, # equivalent to $dbh->do_something, if $dbh were a Company $dbh->Company::do_something;

    Caution: Contents may have been coded under pressure.