--- Marcus Willemsen <[EMAIL PROTECTED]> wrote:
> Hi all,
> 
> can anybody help?
> I am trying to set up a form using a hash of hashes to build different 
> types of fields as I think it is easier to check and process form input:
> 
> my %fields = (textfield => { 'Name_of_textfield' => { -name => 'bla' },
>                                 'Name_of_next_textfield' => {-name => 'blo'}
>                                 },
>                  textarea => {'Name_of_textarea' => {-name => 'blu' }
>                                 },
>                  scrolling_list => {Name_of_list' => {-name => 'ble',
>                                                        -values => ['1', 
> '2', '3'],
>                                                            -default => ['1']
>                                                         }
>                                                }
> );
> This hash of hashes is passed to a subroutine to display the from when the 
> script is called on for the first time. The idea was to use the keys of the 
> hash as tags for the html document.
> 
> sub form
> {
> my $field_ref = shift;
> print start_form (-action => url());
> foreach my $tag (keys (%{$field_ref})){
>      my $name = (keys(%{$$field_ref{$tag}}));
>      my $record = $$field_ref{$tag}{$name};
>      my $label = $record->{label};
>                        print ($tag (-name => $record->{name}));
>                    }
> print submit (-name => "choice", -value => "Send"),
>      end_form();
> }
> 
> It doesn't work I keep getting a "Can't use string ("scrolling_list") as 
> symbol ref while "strict refs" in use at line number xy.
> But it doesn't work either if I use "no strict 'refs'" in the subroutine. 
> Then only the submit button is displayed.
> I suppose it has something to do with hard vs symbolic references but to be 
> honest I didn't get that part in the perldocs.
> Is there a better way to solve the problem?
> 
> Thanks Marcus

Marcus,

Several points:

1.  You are correct that strict is trying to tell you something here.  One point to 
keep in mind
that strict *usually* is trying to prevent you from doing something that you really 
don't want to
do.  There are exceptions, but usually I find that programs that ignore strict do so 
for all the
wrong reasons (for those that do so for the right reasons, read just about any module 
written by
Damian Conway).

2.  As for using a hash to create a form, this probably isn't going to work well as 
hashes store
their data in what is essentially an unordered state.  I assume that you don't want 
your form
elements to show up in random order :)

3.  Maybe it's just my email client, but it looks like you have arbitrary indentation. 
 While
consistent indentation is no guarantee of good code, inconsistent indentation is 
usually
indicative of bad code.  This is because the person coding is new to it and doesn't 
have an
identation style, the person is rather unclear regarding the scope of their code (and 
it can be
hell trying to determine the scope of a variable when code is poorly indented.  
Consider the
difference here:

--------------------------------
my %fields = (textfield => { 'Name_of_textfield' => { -name => 'bla' },
                                'Name_of_next_textfield' => {-name => 'blo'}
                                },
--------------------------------
Versus:
--------------------------------
my %fields = (
    textfield => { 
        Name_of_textfield      => { -name => 'bla' },
        Name_of_next_textfield => { -name => 'blo' }
    },
--------------------------------
The second example, while it doesn't suit everone's indentation style, is still 
clearer to read.

Now, if you still want to try to build forms in this method, I would use an array to 
preserve
order and coderefs:

    #!/usr/bin/perl -wT
    use strict;
    use CGI qw/:standard/;
    
    my @form;
    my @fields = (
        {
            function => \&textfield,
            args => {
                -name => 'username',
                -size => 20
            },
        },
        {
            function => \&password_field,
            args => {
                -name => 'password',
                -size => 20
            },
        },
        {
            function => \&checkbox,
            args => {
                -name => 'saveinfo',
            },
        }
    );
    
    my $html = '';
    foreach my $hashref ( @fields ) {
        $html .= $hashref->{ function }->( $hashref->{ args } );
    }
    print $html;

That's going to be a bit confusing, though, and some may not find it easy to maintain.

Cheers,
Curtis "Ovid" Poe

=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__________________________________________________
Do You Yahoo!?
Send your FREE holiday greetings online!
http://greetings.yahoo.com

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

Reply via email to