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

Hello Monks, I really need some help and I'm sure like always it's something simple, but I just cant figure it out tonight for some reason. Basically I have a very basic index.cgi file that runs 2 sql queries and should eventually send that data into the tmpl file to be looped. Problem is I apparently have an issue with 1 of my 4 values being passed to the file because the following error is triggered apon execution of index.cgi:

HTML::Template->output() : fatal error in loop output : HTML::Template : Attempt to set nonexistent parameter 'loc_value' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1) at /usr/lib/perl5/site_perl/5.8.3/HTML/Template.pm line 2962 at index.cgi line 17

I turned off die_on_bad_params just to see what would happen and my data from @network_data is there, but @location_data seems to be added as whitespace onto the NETWORK_DATA loop!

Here is the top part of my index.cgi script:
#!/usr/bin/perl -w use strict; use HTML::Template; use DBI; ##DB CALL## my (@network_data, @location_data) = get_data(); ##Template## my $template = HTML::Template->new(filename => 'main.tmpl'); $template->param( NETWORK_DATA => \@network_data); $template->param( LOCATION_DATA => \@location_data); print "Content-Type: text/html\n\n", $template->output;
I have tested the data in @network_data & @location_data and the data was there. Also when I append @location_data to @network_data the data makes it through to the webpage as well. To help visualize these LOOP's populate HTML drop downs. Here is the relavent parts of my main.tmpl file.

main.tmpl:
IP: <select name="network" id="network"> <option value="None" selected>None</option> <TMPL_LOOP NAME="NETWORK_DATA"> <option value="<TMPL_VAR NAME=NET_ID>"><TMPL_VAR NAME=NET_ +VALUE></option> <br> </TMPL_LOOP> </select> <select name="location" id="location"> <option value="None" selected>None</option> <TMPL_LOOP NAME="LOCATION_DATA"> <option value="<TMPL_VAR NAME=LOC_ID>"><TMPL_VAR NAME=LOC_ +VALUE></option> <br> </TMPL_LOOP>
These loops are not within each other and I dont care whether or not they can access each other's data. I am simply trying to have essentially 2 while loops with 2 data sets that loop through and print out and for some reason LOC_VALUE is triggering an error. I've switch around so many things I can't remember any more helpful information to tell at this time.

This is how and what Im stuffing my array's:
while (my $ref = $sth->fetchrow_hashref()) { my %row_data; $row_data{NET_ID} = $ref->{'id'}; $row_data{NET_VALUE} = $ref->{'value'}; push(@network_data, \%row_data); } while (my $ref2 = $sth2->fetchrow_hashref()) { my %row2_data; $row2_data{LOC_ID} = $ref2->{'id'}; $row2_data{LOC_VALUE} = $ref2->{'value'}; push(@location_data, \%row2_data); }

I REALLY appreciate any help I can get.

Thanks,
YpCat

Replies are listed 'Best First'.
Re: HTML::Template Error
by tinita (Parson) on Jul 24, 2004 at 13:01 UTC
    my (@network_data, @location_data) = get_data();
    after this assignment @location_data is empty. try references:
    my ($network_data, $location_data) = get_data(); # loop over @$network_data for example
    read perlref for details.
Re: HTML::Template Error
by dws (Chancellor) on Jul 24, 2004 at 16:05 UTC

    On the surface, you've run into a well-known Perl pitfall: Multiple simultaneous array assignments put everything into the first target array, and starve the others. That's an easy problem to solve, but I don't think moving to references is the right approach, because it doesn't address the more subtle structural issue of accidental coupling via get_data().

    I think you'll be happier off switching to:

    my @network_data = get_network_data(); my @location_data = get_location_data();
    rather than than collecting up all of your data and returning it from one function call. The problem with get_data() is that it makes the code brittle. If you later decide to add another type of data, you have to track down all callers of get_data() and teach them that there's another data set in the list of references that get_data() returns. Even if you only call get_data() once, there's still the chance that you'll get the order wrong, since get_data() doesn't provide any clues about the order of the data sets it returns. This hidden order dependency is a form of "coupling", which is a Software Engineering term that generally means "A Bad Thing, Don't Do It."

    By having a separate function for each data set, it's easy to add a third without interfering with the first two. Order dependency isn't an issue. This might not be an issue for the current script, but it's something to keep in mind when you find yourself writing something larger.

      Thank You both very much. I just realized how silly I am. I am aware of multiple lists when being returned being flattend. I just managed to oversite it at 5 in the morning + I was convince there was something going on with HTML::Template so I didn't even bother to realize. It's clear and day now and the issue has been resolved. I used 2 different sub routine's to return the 2 queries and now everything is peachy.

      Thanks Again,
      YpCat