Hi,
I'm having problems with accessing class data from inherited classes. I have a base class called IDObject, which contains general database access routines for a number of tables sharing a set of properties. I then subclass IDObject to specific classes which implement functionality for specific tables in my database. The database access is handled via another class called DB, which is basically a wrapper around DBI with some extra stuff in it.
The problem: I want to let my program instantiate the DB connection, and then set a class variable in the IDObject class to store the reference to the DB object in. This is done via a function called set_db() which is defined like this:
package IDObject;
my $db;
sub set_db {
$db = shift;
}
I can use the $db object without problems in the methods defined in the IDObject class. However, when I try to use it from a class that inherits from IDObject:
package Table;
use DB;
@ISA=qw(DB);
sub fix_it {
my $this = shift;
my $what = shift;
my $results = $db->search($what);
.
.
}
I get error messages about not being able to execute search through and unblessed reference (I don't have the exact message here).
I've tried using
$SUPER::db,
$this->{$db} and even
$IDObject::db, but none of them works.
Does anyone know how to solve this, or is this perhaps the wrong way to go about it? I know I can call my table class constructors and include the DB reference in the call. This looks ugly though, and it forces me to keep the DB reference in a global in my program.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.