Here's a function I wrote to make this eaiser for myself... Might be
usefull, might not.  It depends on you having a hash of data you want to
dump into a cgi table.

sub HashToTable
  {
    my $hashRef = shift;
    my $orderRef = shift;
    
    my $table_contents;
    
    if($orderRef)
      {
        foreach my $key (@$orderRef)
          {
            my $value = $hashRef->{$key};
            $table_contents .= Tr(
                                  td({-valign => 'top'}, b($key)) .
                                  td((ref $value eq
'HASH')?(HashToTable($value)):($value))
                                 );
            
          }
      }
    else
      {
        while (my ($key, $value) = each %$hashRef)
          {
            $table_contents .= Tr(
                                  td({-valign => 'top'}, b($key)) .
                                  td((ref $value eq
'HASH')?(HashToTable($value)):($value))
                                 );
          }
      }
    return table({-align=>'center', -border=>2, -bgcolor=>'FEFFE'},
$table_contents);
  }



I call this in many cases.  Here's one example used in a form:

    $html .= start_form();
    $html .= HashToTable({Name=> textfield(-name=>'NAME'),
                          Type=> textfield(-name=>'TYPE'),
                          Username=>textfield(-name=>'USERNAME'),
                          Password=>textfield(-name=>'PASSWORD'),
                          Server=>textfield(-name=>'SERVER'),
                          Description=>textarea(-name=>'DESCRIPTION'),
                         }, 
                         ['Name', 'Type', 'Username', 'Password', 'Server',
'Description']);

Nice thing is that it's a recursive function, so if you have a hash where
some of the values are other hashes, this will resolve those out into nested
tables.

The ordering array ref that is the second parameter is optional.  Since you
can't assume ordering on a hash.

I too would like suggestions on this approach if possible!

Bruce W. Lowther
OpenAuto Lead
Micron Technology, Inc.
Boise, Idaho
[EMAIL PROTECTED]


-----Original Message-----
From: Andrea Holstein [mailto:[EMAIL PROTECTED]]
Sent: Saturday, November 10, 2001 3:44 PM
To: [EMAIL PROTECTED]
Subject: Re: CGI.pm Question


Jess Balint wrote:
> 
> Hello all, I was wonder if there is a way to do this with less
`$query->`'s.
> And yes, I know the table's not perfect. Any input would be appreciated.
> Thanks.
> 
> ~Jess
> 
>         print   $query->start_form,
>                 $query->start_table({-border=>undef}),
>                 $query->Tr({-align=>CENTER,-valign=>CENTER}),
>                 $query->td(["Username: "]),
> ...

If you want to use OO-way still, there's a funny trick to short your
source-code:

for ($query) {
        print   $_->startform,
                $_->start_table({-border => undef}),
                $_->Tr({-align=>CENTER,-valign=>CENTER}),
                $_->td(["Username: "]),
                ...
}

A second way (not so funny :-( ) is to use run time code evaluation:

my @queries = qw/
        start_form,
        start_table({-border=>undef}),
        Tr({-align=>CENTER,-valign=>CENTER}),
        td(["Username:\ "]),
        ...
/;

eval "print " . join(",", map( {'$query->' . $_}, @queries )) . ';';


Best Wishes,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to