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

Has anyone done any database + html template programming with perl?
I really need some help with this code, i tried very many variations of this and I still can't get it to work.
my @domains; while (my $row = $sth->fetchrow_hashref) { %aaa = %$row; #deref push @domains, $aaa; print $aaa{'subdomain'} . "\n"; #returns www } print @domains[0]{'subdomain'} . "\n"; #doesn't work ?? $template->param( DOMAINS => @domains #Doesn't work either # example from the manual goes as follows: #$template->param(EMPLOYEE_INFO => [ # { name => 'Sam', job => 'programmer' }, # { name => 'Steve', job => 'soda jerk' }, # ] # ); print $template->output;

Replies are listed 'Best First'.
RE: I really need help with perl data structures and HTML::Template
by lhoward (Vicar) on May 29, 2000 at 00:31 UTC
    Well, I see a few problems here. First off when you're doing push @domains,$aaa; you're not pushing the value of the hash %aaa on to @domsons, you are pushing the undefined scalar $aaa onto @domains. use strict would have helped you pick this up. Try this instead:
    while(my $row=$sth->fetchrow_hashref()){ push @domains, $row; }
    then you can iterate through @domains like this. And you test print statment will work (formated slightly different).
    print "$domains[0]->{subdomain}\n"; my $d; foreach $d(@domains){ print "$d->{subdomain}\n"; }
    Then calling HTML::Template with this code:
    my $template=HTML::Template->new(filename=>'test.tmpl'); $template->param(DOMAINS => \@domains); print $template->output();
    and this template
    <TMPL_LOOP NAME=DOMAINS> Subdomain: <TMPL_VAR NAME=SUBDOMAIN> </TMPL_LOOP>
    produces the desired result (at least off of my DB and PERL).

    Most of the problems you are having seem to be related to perl datastructures and references. When you get a chance you should read the perldsc and perlref documents that come with perl. They should put you on the right track with datastructures and references

Re: I really need help with perl data structures and HTML::Template
by lhoward (Vicar) on May 29, 2000 at 02:25 UTC
    This is a repost or my reply since the original post was double-posted and the one that my reply was originally attached to was cleaned up :)
    Well, I see a few problems here. First off when you're doing push @domains,$aaa; you're not pushing the value of the hash %aaa on to @domsons, you are pushing the undefined scalar $aaa onto @domains. use strict would have helped you pick this up. Try this instead:
    while(my $row=$sth->fetchrow_hashref()){ push @domains, $row; }
    then you can iterate through @domains like this. And you test print statment will work (formated slightly different).
    print "$domains[0]->{subdomain}\n"; my $d; foreach $d(@domains){ print "$d->{subdomain}\n"; }
    Then calling HTML::Template with this code:
    my $template=HTML::Template->new(filename=>'test.tmpl'); $template->param(DOMAINS => \@domains); print $template->output();
    and this template
    <TMPL_LOOP NAME=DOMAINS> Subdomain: <TMPL_VAR NAME=SUBDOMAIN> </TMPL_LOOP>
    produces the desired result (at least off of my DB and PERL).

    Most of the problems you are having seem to be related to perl datastructures and references. When you get a chance you should read the perldsc and perlref documents that come with perl. They should put you on the right track with datastructures and references

Re: I really need help with perl data structures and HTML::Template
by perlmonkey (Hermit) on May 29, 2000 at 01:31 UTC
    Whenever I start using any data structures, I use Data::Dumper profusely. Data::Dumper will simply print out the entire data structure with the proper reference notation. It is a great way to start debugging scripts when you dont understand the data you are dealing with. Of course lhoward's advice is definately more valuable: use strict. Here is an example of Data::Dumper
    use Data::Dumper; my %hash = ('a'=>1, 'b'=>2, 'b'=>3); my @array = ('c','d','e','f'); my $data = {%hash}; $data->{'array'} = [@array]; $data->{'array'}[4] = {%hash}; $data->{'array'}[4]{'foobar'} = [@array]; print Data::Dumper->Dump([$data], ['data']);
    Results:
    $data = { 'a' => 1, 'b' => 3, 'array' => [ 'c', 'd', 'e', 'f', { 'foobar' => [ 'c', 'd', 'e', 'f' ], 'a' => 1, 'b' => 3 } ] };
    And you can use it to print out any unknown object, such as a CGI object:
    use CGI qw(:standard -debug); my $cgi = new CGI; print Data::Dumper->Dump([$cgi], ['cgi']);
    I entered in values of 'foobar=val, and 'submit=sumbit' and the Results:
    $cgi = bless( { '.charset' => 'ISO-8859-1', 'foobar' => [ 'val' ], '.parameters' => [ 'foobar', 'submit' ], 'submit' => [ 'sumbit' ], '.fieldnames' => {} }, 'CGI' );
    The notations for {} mean hash references and the notation for [] is for array references.