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

I'm trying to write a small CGI script which will allow a user to add records to a database by filling in an html form. The form is generated dynamically by getting all the columns from the database along with their types and maximum lengths (where appropriate). The input type is then converted into something suitable for hmtl input (since the database has more then one type of field for which the input is best represented as a textfield in the html) and stored in a hash with column names as keys and the input types as values. The form is generated within a table and should call on the hash to get the right type for the query like this:

print start_Tr, start_td; print "$req$key: "; print end_td, start_td ($query->$input_html{$key}("$key") ); print end_td, end_Tr;

I'm aiming for something like:

<tr> <td>$req$key:</td> <td> <input type="$input_html{$key}" name="$key"> </td> </tr>

But this gives me a syntax error near "$input_html{". Is this because what I'm trying to do here is not allowed and if so, how can I work around it?

Replies are listed 'Best First'.
Re: Using hash values as CGI query types
by shenme (Priest) on Jan 28, 2005 at 12:06 UTC
    Ahh, translation is "You want to dynamically pick which method of the CGI.pm object to call, by using the name of the method from a variable."

    And what you're asking is how to do the below, which works, in one step:

    my $sub_is = $input_html{$key}; start_td $query->$sub_is(-name=>'$key');
    Its too early in the morning here, I can't see the better way to do this.
      Thank you! That is just what I need, and now I feel really silly for not thinking of just doing it like this! Sorry for the badly phrased question and thank you for taking the time to decipher it :D
Re: Using hash values as CGI query types
by davis (Vicar) on Jan 28, 2005 at 12:05 UTC
    CGI doesn't have the method (start_td etc) that you're using
    You need to pass in each value to methods such as td, Tr etc:
    #!/usr/bin/perl use warnings; use strict; use CGI qw(:standard); my $foo = "bar"; my $bar = "baz"; #i've passed Tr an _array reference_ print Tr([td($foo), td($bar)]), "\n";
    You might also want to check out something like HTML::Template or Template::Toolkit
    Update: after your update:
    print Tr( [ td($req.$key), td(input(-type=>$input_html{$key}, -name=>$key)) ] );
    Should do what you want (untested). However, writing this makes me think that there's a Better Way of doing what you're trying to do.

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
      In the CGI.pm doc, check under section "SPECIAL FORMS FOR IMPORTING HTML-TAG FUNCTIONS" $query->start_td works fine, though you are right that using a bare start_td won't, requiring you to mention the need in the use, ala
      use CGI qw( :standard *td );
        Didn't know that. Well, you learn something new every time you remember something you didn't know before :)

        davis
        It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.