in reply to Inheritance and module cross dependencies

Go over your code replace all your calls to Teacher's constructor with a factory method that returns a teacher that can actually be a Lecturer. (Seminary?) Obviously the factory must have enough information when it is called to know what kind of teaching it is generating.

Since the rest of the code just assumed Teacher, it should continue to work with a Lecturer (which ISA Teacher). When you do need ->get_research_projects() you can use can to check if it is safe to call it.

Avoid saying Lecturer explicitly in your code, to what degree this is possible.

Replies are listed 'Best First'.
Re^2: Inheritance and module cross dependencies
by prowler (Friar) on Jan 10, 2005 at 07:29 UTC

    Thanks.

    Now to work out how best to do this without breaking anything...

    Prowler
     - Spelling is a demanding task that requies you full attention.

      Plan:
      1. Create TeacherFactory.pm:

        package TeacherFactory; use Teacher; use strict; sub create_teacher { my ($class, @args) = @_; return Teacher->new(@args); } 1;
      2. Make a list of all files that call Teacher->new.
      3. Insert near their tops a use Teacher; statement.
      4. Search and replace teacher constructions with factory requests.

      That's the easy part; you have a pretty boring factory. Now make it interesting :)

      Most likely, you may find that some calls to Teacher->new give you insufficient information to make the decision about whether a Lecturer or a teacher is required. Refactor your code *after* you have the above in place to either defer or advance the construction, depending on what makes more sense. But move in small steps that don't leave your code unstable.