Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Generalizing table display for subclass

by jest (Pilgrim)
on Jul 29, 2003 at 08:32 UTC ( #278769=perlquestion: print w/replies, xml ) Need Help??

jest has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Generalizing table display for subclass
by PodMaster (Abbot) on Jul 29, 2003 at 09:03 UTC
    How do you invoke this? (what do I plug in to get results -- you can't expect anyone to figure it out by looking at your code, use Data::Dumper to get *sample* data)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I'm sorry, I'm not entirely sure what to do to be able to supply sample data. This runs through several different modules, each one doing some other thing, and I don't know how to simplify it enough to show here.

      The immediate calling mechanism comes from the superclass, and looks like this, with the $sth coming from another module that handles the database querying:

      my @row; # construct a table header; store in @row push (@row, $self->_get_table_header(\@order)); # construct each table row; add to @row while (my $ref = $sth->fetchrow_hashref()) { # clean this up with named parameter calls? push (@row, $self->_get_table_row(\@order, $ref, $book, $rowcolor)); $rowcolor = ($rowcolor eq "lightrow" ? "darkrow" : "lightrow"); } $sth->finish(); print table ({width=>"95%"},-cellspacing=>"0"},@row);

      Sorry to keep bouncing back and forth like this, but: while I'm still not sure how to generate something from which you could run the code extract, the result of running it would look something like this:

      AuthorTitleDate acquired
      Bekman, Stas, and Eric Cholet Practical mod_perl (2003) 2003-06-30
      Burke, Sean M. Perl and LWP (2002) 2002-07-09
      Conway, Damian Object Oriented Perl (2000) 2000-07-25
      Hall, Joseph N., with Randal L. Schwartz Effective Perl Programming (1998) 2000-11-10

      Except that the links would work, and there would be alternating-colored rows, etc. I don't know if that helps anyone help me, but thank you anyway.

Re: Generalizing table display for subclass
by chunlou (Curate) on Jul 29, 2003 at 14:26 UTC
    Just a minor comment. Since you mentioned superclass and subclass, I'm guessing you meant inheritance. But since, as you stated, your superclass does data retrieval and subclass formatting, it's a delegation relationship, not an inheritance relationship. In Perl, delegation doesn't need be OO at all. But if you misinterpreted you, never mind me then.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://278769]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2022-01-18 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (52 votes). Check out past polls.

    Notices?