in reply to Objects and Inheritance

Upon further reading, I wasn't specific enough, I suppose.

Perl gives me an error upon trying to create a new BaseClass::Log::Writer object

When I step through the code, I get a call to the Writer.pm module's new() sub with "BaseClass::Log::Writer" as the class. This is good.

I then call $class->SUPER::new which takes me into the Log.pm module's new sub. Here's where things are funky. I assume the class should now be "BaseClass::Log", but it's showing as "BaseClass::Log::Writer" which barfs when I call $class->SUPER::new.

Here are the various constructors.

BaseClass Constructor:

package BaseClass; sub new { my $class = shift @_; my $self = {}; $self = bless $self, $class; # Re-read the config file if it has changed... my $mtime = ( stat(CONFIG_FILE) )[9]; if ($mtime != $config_time) { ($config, $config_time) = &_read_config(CONFIG_FILE); } return $self; }

Log.pm Constructor:

package BaseClass::Log; @ISA = qw( BaseClass ); sub new { my $class = shift; my $self = $class->SUPER::new; my (%opt) = ( @_ ); $self->{opt} = \%opt; # Connect to the database. $self->{dbh} = $self->connect_db('hits') || return undef; return $self; }

Writer.pm Constructor

package BaseClass::Log::Writer; @ISA = qw( BaseClass::Log ); sub new { my $class = shift @_; my $self = $class->SUPER::new; my (%opt) = (@_); # Standard fields for the Log module go in this array. @self::_properties = qw( type_id class_id subclass_id severity process_id text_info program_name module_name unix_pid host_name purge_date ); # We can set up some defaults for process and such. $opt{unix_pid} = $$ unless $opt{unix_pid}; $opt{program_name} = $0 unless $opt{program_name}; $opt{hostname} = $ENV{HOSTNAME} unless $opt{hostname}; $opt{module_name} = caller || $opt{program_name} unless $opt{module_name}; $opt{purge_date} = POSIX::strftime("%Y-%m-%d", localtime(time)) unless $opt{purge_date}; $opt{type_id} = 'unknown' unless $opt{type_id}; $opt{class_id} = 'unknown' unless $opt{class_id}; $opt{subclass_id} = 'unknown' unless $opt{subclass_id}; $opt{severity} = 0 unless $opt{severity}; $self->{opt} = \%opt; return $self; }

I hope this makes it a little clearer.

Replies are listed 'Best First'.
(Ovid) Re(3): Objects and Inheritance
by Ovid (Cardinal) on Nov 21, 2001 at 05:20 UTC

    Could you be a bit more specific? I am assuming that the following is not your error message:

    Encountered barf in somescript.pl line 12.

    I just created a small test case and it works just fine for me. Are you sure that all of your classes are useing the classes that they inherit from? If your BaseClass::Log forgets to use BaseClass (or perhaps misspells it?), then you will get an error message similar to the following:

    Can't locate object method "new" via package "BaseClass::Log" (perhaps + you forgot to load "BaseClass::Log"?) at D:/Perl/site/lib/BaseClass/Log.pm line 14.

    Admittedly, that error message can be a bit misleading.

    Incidentally, the class that is passed along will be the class that is initially called. It will not be the class you are calling.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      That was it... I missed a use in the BaseClass::Log file.

      I assumed we'd get cascaded use, which is wrong.

Re: Re: Objects and Inheritance
by runrig (Abbot) on Nov 21, 2001 at 05:21 UTC
    SUPER does not change the class that the called method sees. It is still the the same class you call new with. (i.e. the Writer class).

    Even though you're (eventually) calling the BaseClass new method, it's going to get 'BaseClass::Log::Writer' as the class if called (originally) from the BC::L:Writer new method. The error is just in your head :)