I have written a reasonably complex object in Inline C, and love it.
Our shop has several other datafiles that we regularly write new code
to parse (doing yet another study for yet another question which can be
answered by these files). To speed the writing of this constant stream
of new programs (and the subsequent running of them!), I want to write
C object modules for all these file formats that can use Perl's
inheritance system.

This presents an obvious opportunity to centralize code that's common
between them, such as the method to read the next record, or to pass
back the entire last line read as a string, or to open the file/pipe
for the input. 

It's an in-house setup, so I can create any standards necessary; each
subsequent inheriting module should just need to implement the specific
struct format for that file, and the methods that will parse out the
specific fields, and a few utility and convenience functions that will
expediate processing for that particular file layout.

My question is: how do I write inheritable code?
Currently, the problem plaguing me is that I want the parent class's
record-reading method to directly call the child class's record
*parsing* method. (feel free to point and laugh, as long as you have a
better suggestion. =o)

I created a generic struct with an integer which will hold the address
of the specific file struct allocated per object. The constructor for
each specific object type will malloc the memory for the records to be
read into, and store the location of that address on the generic file
object. I have a define:

#define OBJ ((generic_file_object*)SvIV(SvRV(obj)))

That *should* mean that (char*)OBJ->rec is effectively the struct as a
string. To read into it, I say:

    if (OBJ->fpin) { /* if there's a valid open file pointer */
        if (ptr = fgets(((char*)&OBJ->rec),OBJ->recSize,OBJ->fpin)) {
            OBJ->linectr++; /* increment the line counter */

rec is the struct layout for the particular file, recSize is the size
of that struct, fpin is an already opened file pointer.... So far, so
good?

Ok, not each file needs specific and different initializations, which
should be written into that file's package space. Those inherit from
this one, so if I define an init() function in this package, then
redefine it in that package, ... or do I not define it in the parent?
Would it even compile? >:o/

I get confused as to how the inheritance system interacts with C
scoping. My C is rusty anyway. How do I make sure the parent class's
reading method calls the child class's init() method?

lol!! HELP!!!

Would it be best to just leave them seperate, and then write a Perl
wrapper method that bundles the functionalities?

Hmmm.... that begins to make sense.....

Suggestions?


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Reply via email to