[lug] perl and cgi.pm

Tkil tkil at scrye.com
Fri Feb 8 10:38:48 MST 2002


>>>>> "Michael" == Michael J Hammel <mjhammel at graphics-muse.org> writes:

Michael> This is the easiest way, but not if the values to be used are
Michael> coming from a database and you're getting those by iterating
Michael> through the results of an SQL SELECT statement.

In that case, just use checkboxes with the same name.  That's what a
checkbox group is, anyway.

| while ( my $cur = $sth->fetch() )
| {
|     my ($record_id, $label, $is_checked) = @$cur;
|     my $cb = checkbox( -name    => 'records', 
|                        -value   => $record_id,
|                        -label   => $label,
|                        -checked => $is_checked );
|     print Tr( td( $cb ) );
| }

The only thing I'd want to double-check here is whether this works
well with some of CGI.pm's "behind your back" features, e.g., the
reset button.

Michael> I could do this by changing the flow of my script - grab all
Michael> the data from the db at once and then shove it into the
Michael> checkboxes - but I don't like that flow.  I prefer doing the
Michael> necessary steps on each iteration [...]

Those are good reasons to prefer the build-as-you-go approach.  A
possible counterexample comes from thinking about a heavily-loaded
server situation where you're using database connection pooling.  You
don't want one process keeping a connection in use when it is just
doing purely-internal formatting stuff.

I also like putting most of the database stuff in one place -- that
way, if I ever have to migrate from one DB to another, I know where to
look.

(In one project, I actually added "MySQL" and "Oracle" sub-classes to
all of my modules and objects, insulating the main code from changes
in the database.)

    | my $table = $q->table( $q->Tr( [ map $q->td($_), 
    |                                  $q->checkbox_group( ... ) ] ) );

Michael> If I have time I'll have to explore what this little bit of
Michael> code does.  For now, I just embed td's inside Tr's as
Michael> function alls and pass appropriate args, pushing the results
Michael> into an array.

One advantage to using the implicit map-on-array-ref feature of Tr and
td (and th) is the distribution of attributes across all the values.
Of course, if you're doing this in a loop, that doesn't matter as
much.  

t.



More information about the LUG mailing list