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

I can't figure out what I am doing wrong and so I turn to the monks....

I have a function (see below) that prepares and calls the results of a SQL SELECT for an HTML::Template TMPL_LOOP. The function passes the SQL statement to another function which does the database lookup and returns an array of hashes (each column = 1 hash key). The function below refines the data a bit more (resolves table references into something more understandable for humans) and passes the refined array of hash references onto HTML::Template.

For some strange reason the assignment of the array to the appropriate TMPL_LOOP gives me a 500 error (notated in the code as the line that give me angst). If I comment out this line the page is served without issue (albeit without the results of the SELECT statement).

I put in some diagnostics to make sure there weren't any problems with the array of hash refs just before the assignment and all looked fine. I successfully use TMPL_LOOP in several places throughout my entire script and the syntax is the same everywhere, but this is the only place it seems to be broken.

NOTES: GetListFromDB does the SQL calls and returns the original array of hashes and GetItemFromDB resolves coded entries to strings. If it would help to see them let me know and I will post them. Thank you

I await absolution....

sub ShowLibLog { my $SQL = 'SELECT LibID, UID, ActionID, TXDate, Time, DocName, FromHos +t FROM LibrarianLog'; + my @log = GetListFromDB($SQL); my $item; my @liblog; foreach $item (@log) { # GetListFromDB will store the results of the sql statement in an arra +y of hash # references. Our first step is to create an array of hashes to be pa +ssed to # HTML::Template for display based on the results of the SQL statement +. *PHEW* my %entry = (LibID=>%$item->{'LibID'}, UID=>GetItemFromDB('RealName','UID', %$item->{'UID'},'User'), ActionID=>GetItemFromDB('ActionName','ActionID', %$item->{'ActionID'},'Action'), TXDate=>%$item->{'TXDate'}, Time=>%$item->{'Time'}, DocName=>%$item->{'DocName'}, FromHost=>%$item->{'FromHost'}); push (@liblog, \%entry); } $viewliblog_page->param(liblog=>\@liblog); #This is the line giving me + angst!!! print "Content-type: text/html\n\n", $viewliblog_page->output; }

Replies are listed 'Best First'.
Re: Trouble with TMPL_LOOP assignment
by George_Sherston (Vicar) on Oct 18, 2002 at 17:54 UTC
    This looks like a problem with the data structure you're feeding to HTML::Template. Try
    use Data::Dumper; print Dumper(\@liblog);
    to see what you've got. Also to get interesting info about what went wrong,
    use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); print header;
    at the head of your script will give you the useful HTML::Template error info, inter alia.

    Specifically in your case I may well be missing a bit of syntax, but I think %$item->{'ActionID'} probably ought to be $item->{'ActionID'} - otherwise you'll get an annoyingly interesting data structure? Or, did you initialise $viewliblog_page as an HTML::Template object? - this isn't clear from the chunk you posted

    Also, you could save a bit of effort by modifying the elements in @log rather than creating a new array, as in
    for my $item (@log) { $item->{UID} = GetItemFromDB('RealName','UID',$item->{'UID'},'User +') $item->{ActionID} = GetItemFromDB('ActionName','ActionID',$item->{ +'ActionID'},'Action'); }

    George Sherston
Re: Trouble with TMPL_LOOP assignment
by krisahoch (Deacon) on Oct 18, 2002 at 19:24 UTC


    First and formost, let us see the 'GetListFromDB' function. You can edit out all sensitive data, but we need to be able to see what you are doing to get your return for my @log = GetListFromDB($SQL);

    Secondly, show use your my $viewliblog_page = new HTML::Template declaration so we can see what is going on there (albeit there is probably nothing wrong there or you'd have bombed out way before the angst line)

    Thirdly, try this at the end.

    $viewliblog_page->param(liblog => [ @liblog ]

    One last thing... Let us see your template file


    Kristofer Hoch

Re: Trouble with TMPL_LOOP assignment
by zigdon (Deacon) on Oct 18, 2002 at 17:03 UTC
    For some strange reason the assignment of the array to the appropriate TMPL_LOOP gives me a 500 error

    what's the error message from the error log of the webserver? Did you make sure that your template actually has a "liblog" loop?

    -- Dan

Re: Trouble with TMPL_LOOP assignment
by spork (Monk) on Oct 22, 2002 at 13:56 UTC
    Thank you for the suggestions. It still does not work, but I am going to rewrite pieces of the code to make this functionality more generic since it will be used in other places in my script.

    I plan to call GetListFromDB with an additional parameter. Specifically I am going to pass the name of a function that will (based on the SQL statement) build a specific hash table and then I will leave it up to the GetListFromDB to build the array of hash refs for HTML::Template. Perhaps in the rewrite I will fix whatever I broke (I can dream).

    A few questions were asked about the original posting that I would like to answer. There was no message in the error_log file indicating a problem. However the array I am trying to create to pass to HTML::Template was part of the header (VERY odd). Perhaps I can unstep on the toes I have stepped on in the rewrite.

    Thanks again for your responses. Now on with my coding.