|Perl: the Markov chain saw|
Re: Beginner OOP style questionby Tardis (Pilgrim)
|on Mar 02, 2002 at 08:39 UTC||Need Help??|
Thankyou! I hardly expected such an insightful reply so quickly!
What you've said makes loads of sense. Firstly, regarding the class method, I give myself a big forehead slap, and say 'Doh' for that. I should have made that intuitive leap.
Secondly the search. Again, this makes lots of sense. I did think of this but initially discarded it, since my backend is SQL and I thought this would be costly in time and memory (each object could be several K worth of text).
But after reading your words and some reflection, I think this is mitigated by two factors:
BTW, just some background on my little project, may be interesting to you or others, and comments are as always welcome. I'm designing my modules to support on online journal, the 'dear diary' kind.
I have implemented this several times over the last few years, the earliest being a .BAT file and a bunch of editor macros. The most recent and still in use is a PHP version.
So to bring it into the OOP context, each 'Entry' object holds some meta information like author (probably will be another object), date written and so forth, plus the actual text.
The part that made me think it would be an ideal bench test for my OOP learning experience was that I wanted to implement a DBM backend later, to replace the SQL backend. If done right, I simply copy Backend.pm-DBM over Backend.pm and suddenly my appliction no longer needs an SQL database. I also don't need to touch Entry.pm, or any of the other modules.
This comes from a pet peeve of mine, the amount of free software out there which requires a MySQL server, yet doesn't really need one. I'd like to release something that gives the user a choice.
Anyway, that's enough raving about my project, I'm off to go and think about how to implement my load on demand Journal Entry objects.
Both the class search method, and the on-demand fetching are both working a treat.
For the latter, basically I just renamed my fetch method to real_fetch, and made a new fetch method, which simply sets an fetch_ID field in the object.
When my AUTOLOAD proxy method gets called when something tries to access the object data, the method sees that it is necessary to fetch it, and passes the fetch_id to the real_fetch method, which goes out to the backend to do the grunt work.
Great! With the above working, the search class method can return umpteen objects back to the program, without incurring any database access for each one. As soon as the program tries to get data out of them, it is fetched automatically.