in reply to Dealing with design -- inheritance + exporting gone bad :(..

That looks confusing, but so is your description of what it's supposed to do.

It looks like you're exporting subroutines into derived classes. Which you shouldn't have to do, since derived classes already inherit their methods from the base class.

Any way, if that's exactly what's in your code and this is one file then the immediate problem is you're use()ing "pm::Section" before it's defined, so pm::Section->import() is not called. Move pm::Section into it's own pm/Section.pm file.

update: the above paragraph probably isn't right, since this code would abort when trying to load pm/Section.pm, unless you've also got that file but you're defining the rest of the package here.

Also, since you're defining your own import() method, setting @EXPORT_OK & friends in pm::Section won't do a thing.

Actually, everything in pm::Section::import and pm::Section::inheir_merge looks very suspicious.

  • Comment on Re: Dealing with design -- inheritance + exporting gone bad :(..

Replies are listed 'Best First'.
Re^2: Dealing with design -- inheritance + exporting gone bad :(..
by yaneurabeya (Novice) on Jun 19, 2007 at 23:07 UTC

    Q:Why are you exporting a method from a base class to an inheriting class? Normally you either export, or you inherit.

    A: It didn't seem like exporting was working properly (similar error came up as shown in the first post).

    Q: Why are you exporting your import() method?

    A: That was a bad idea -- I fixed that later after receiving your suggestion -- no one in my group honestly knows how to use export (just another guy and I know Perl, and it's somewhat limited to say the least in many areas), and it wasn't 100% clear in the Perldoc what to do with Exporter, until you clarified that point. :(..

    Q: Why are you even defining an import() method when the one you're inheriting from Exporter should work fine and yours does not? I.e. remove the import() method and fix the obvious syntax errors and you can use your inheir_merge() method from the use()ing package.

    A: I tried that and the call failed to do that properly before, but I'll try again..

    Q: Why are you creating an an object of the caller's class to call SUPER on when you can call caller()->SUPER::inheir_merge() directly?

    A: Right now I was just trying to get the call tree down properly so that I could verify that everything was working like it was supposed to. Next I was going to fill in the blanks in terms of merging the data elements together, instead of having all of the keyed hash items straight out replace one another.

    That's most likely not correct though now that I think about it because it will find the inheir_merge() subroutine at the bottom of the call stack, since I won't redefine it anywhere else, and thus it won't do what I want it to do.

    Q: And finally, what is all this supposed to do that you can't solve with straightforward (mutiple or single) inheritance?

    A: It's the merging part that's the cruxt and the issue in the whole problem. Instead of replacing a keyed hash element I want to append in some cases, search and replace arguments in some other cases, append to arrays in yet other cases, etc.

      Why are you exporting a method from a base class to an inheriting class? Normally you either export, or you inherit.

      Why are you exporting your import() method?

      Why are you even defining an import() method when the one you're inheriting from Exporter should work fine and yours does not? I.e. remove the import() method and fix the obvious syntax errors and you can use your inheir_merge() method from the use()ing package.

      Why are you creating an an object of the caller's class to to call SUPER on when you can call caller()->SUPER::inheir_merge() directly?

      And finally, what is all this supposed to do that you can't solve with straightforward (mutiple or single) inheritance?

      update: ah, I see a bit more. You'll probably just want to export properies. Since all of your properties seem to be class -based (i.e. no object-specific properties) you can for instance declare them as methods:

      Flies: Earthbound = 0; Machine: Metallic = 1; Warm-blooded: Blood = "warm"; Kryptonian: Weakness = "Kryptonite";
      package Base; # define default values sub earthbound { 0 } sub metallic { 0 } sub blood { "cold" } sub weakness { undef } # update 2: sub validate { my ($class) = @_; return ($class->blood eq 'warm' or ! $class->metallic); } # define 'mixins' - don't subclass these, # they'll export their properties when you use them package Flies; use base Exporter; our @EXPORT = qw(earthbound); sub earthbound { 1 } package Robot; use base Exporter; our @EXPORT = qw(metallic); sub metallic { 1 } # etc... package Superman; use base Base; use Flies; use Robot; # superman's very metallic.
        Lol. That's about right.. If I wanted the object version though all I would do is export the hash then?