Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm doing some chemistry/physics stuff, and have written 5 OO modules. Four of them inherit from the fifth. Materia::Variable is the base (which inherits from the CPAN module Number::WithError), and the other 4 are: Materia::Variable::Generic, Materia::Variable::Pressure, Materia::Variable::Temperature and Materia::Variable::PressureTemperature.

The 4 sibling modules use base qw(Materia::Variable), which I believe is the correct thing.

At the beginning of a program which uses these 4 modules, I have the 4 use Materia::Variable::... statements. A little ways into the program, the first object I make happens to be ...Generic. It works fine. The next line tries to create a ...Pressure object. And I get:

Can't locate object method "new" via package "Materia::Variable::Pressure" ...

The new methods in all of these classes do very little. Most of the work is done in an _init() method in the base class. @INC includes the directory where all this stuff lives. How come it is forgetting that the module exists? How do you debug this? Single stepping in emacs/perldb is getting me nowhere.

Thanks.

Replies are listed 'Best First'.
Re: OO: short memory?
by plobsing (Friar) on Jan 27, 2008 at 23:16 UTC
    To quote perl:

    perhaps you forgot to load "Material::Variable::Pressure"?

    With that as part of the debug message, it should be obvious that either your code does not load your Material::Variable::Pressure module or said module does not have this method defined. Stepping through in perldb might be a little late to catch the error as this is more of a BEGIN-time bug if the module isn't being loaded.

    Of course this is all speculation until you SHOW US YOUR CODE!
      As I said, at the beginning of the program are the 4 use statements.
      #!/usr/bin/perl -w use strict; use lib '/home/ghaverla/src/perl/lib/perl5'; use DBD::SQLite; use Materia::Variable::Generic; use Materia::Variable::Pressure; use Materia::Variable::Temperature; use Materia::Variable::PressureTemperature; use Data::Dumper;

      I really don't want to input the 9751 lines of code in this program. :-)

        Just curious, but how did you get 9,751 lines into your code before discovering that you couldn't create an object?

        Does Materia::Variable::Pressure have a new method? If not does Materia::Variable have a new method? If there is a new method can you reduce the problem to a simple case, for example cutting out all the methods except new?

        Well of course, you don't need to show all your code, just the relevant stuff. Such as:
        • The loading statements (as you have provided, thanks)
        • The call site that fails (with any relevant surrounding context)
        • The package and loading statements of the module in question
        • The method in question
        If there's something thats a bit long, you can always use the <readmore> tag.

        If you don't give us a feel of what you're working with, its akin to asking us to wave a magic wand to fix it. Despite mentions of magic and wizardry, there are in fact very few Perl wizards.
Re: OO: short memory?
by starX (Chaplain) on Jan 27, 2008 at 23:53 UTC
    Since you're absolutely sure your @INC is finding the module properly, are you sure that ::Pressure includes a new() method?

    --starX
    axisoftime.com

Re: OO: short memory?
by cdarke (Prior) on Jan 28, 2008 at 09:33 UTC
    Maybe you didn't pickup on starX and hipowl. The usual reason for such a message is that you don't have a subroutine called new in your package. In Perl, new is not a keyword or language construct, it is the name of the constructor method. Actually it can be called anything you like, not just 'new'.