Recently I've been reading TheDamian's wonderful book: "Object Oriented Perl" and it got me thinking about the following standard usage of Class::DBI. The meditation is probably neither here nor there.

To create a simple class Foo derived from Class::DBI, one can do something like:

package Foo; use strict; use Class::DBI; use base 'Class::DBI'; Foo->set_db('Main',"url","user","password"); Foo->table('Foo'); Foo->columns(All=>qw/id name/); sub method1{} sub method2{}
Now to get an instance of the Foo class, I can simply call the retrieve() method implemented in the base class(i.e.,Class::DBI), and call any method on the derived class(i.e., Foo):
my $foo = Foo->retrieve(1); # for row with id 1. $foo->method1();
This is all wonderful and natural, for perl. But I realized I can't do that easily in a static language, say Java: if retrieve() is defined/implemented in the base class, its return type has to be the base class, so unless one explicitly cast the returned object to the derived class, one can't call any method of the derived class. In fact, one can look at a general persistent framework like Hibernate, the query/finder method always requires casting ( the save() method is ok since it returns void.) One can further look at EJB, where the finder method is always part of a bean's home interface, there can't be a general finder method (unless some casting is involved). I always feel it's bad design if one has to do much casting. This seems to be at least one advantage of OO for dynamic languages: there is no static type to restrict what method you can call, so you can have at least a general finder method.

In reply to perl, Class::DBI and OO for dynamic languages by johnnywang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.