Oh wise monks, I seek enlightenment.

I'm working on Yet Another app to display database info on the Web. I had originally written it in a highly repetitive way, with much copying of code across related programs, and now as I learn more about OO programming, I am trying to go through and clean it up by modularizing whatever I can. A part that I'm especially unhappy with is this, which is the main set of routines to display results. I've subclassed it, so that this class handles the display for any particular database, with the superclass handling the retrieval of info from the database, etc. Still, it's clear that even this is much bigger than it needs to be, and it's hard to make simple modifications to change the format, to generalize the code, etc. And it seems that there should be a better way to connect the columns to query with the fields displayed. I'd be grateful for suggestions for how to hide away any of this so that the subclass can be more streamlined.

I'm only showing the relevant bits, but it should be enough for this purpose. Also, I know that there are about six zillion templating systems or the like that would make this easier, but for now, as I learn, I'd rather do it myself.

sub get_column_list { # return columns (to superclass) for the database to grab my @columns = ( qw (id author title pub_date date_purch) ); return join ", ", @columns; } sub _get_table_header { my $self = shift; my $order_ref = shift; my %header; $header{author} = td({-width=>"30%"},u(b("Author"))); $header{title} = td({-width=>"*"},u(b("Title"))); $header{date_purch} = td({-width=>"10%"},u(b("Date acquired"))); # use hash keyed to the order, to print results # based on desired order of display return Tr( $header{@$order_ref[0]} . $header{@$order_ref[1]} . $header{@$order_ref[2]} ); } # end _get_table_header sub _get_table_row { # clean this up--named params? my $self = shift; my $order_ref = shift; my $ref = shift; # reference to single result--rename my $book = shift; my $rowcolor = shift; my %cell; my $author; $author = $ref->{'author'} if $ref->{'author'}; $author = $author ? escapeHTML($author) : " "; $cell{author} = $author; my $pub_date = $ref->{'pub_date'} if $ref->{'pub_date'}; $pub_date = $pub_date ? escapeHTML($pub_date) : "&nbsp"; my $title; $title = a({-href=>"$book->{library}_displayfull.cgi?id=$ref->{id}"} +, i(escapeHTML($ref->{'title'})) . " (" . $pub_date . ")"); $cell{title} = $title; my $date_purch; $date_purch= $ref->{'date_purch'} if $ref->{'date_purch'}; $date_purch = $date_purch ? escapeHTML($date_purch) : " "; $cell{date_purch} = $date_purch; return Tr({-class=>"$rowcolor"}, td($cell{@$order_ref[0]}), td($cell{@$order_ref[1]}), td($cell{@$order_ref[2]}) ); } # end _get_table_row

Thank you for your wisdom.


In reply to Generalizing table display for subclass by jest

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.