Hawkes, Mick I <mailto:[EMAIL PROTECTED]> wrote:
: I am having trouble getting a subprocedure to work using : parameter data passed with shift Your call to the GetOfficers() looks suspect twice. First calling it with a reference ( \GetOfficers() ) doesn't look right. Have you tested the results with a test return? #!/usr/bin/perl use strict; use warnings; use Data::Dumper 'Dumper'; my %foo = ( db_loop => \GetOfficers() ); print Dumper \%foo; sub GetOfficers { return ( 1 .. 9 ); } __END__ I get: $VAR1 = { 'SCALAR(0x224f84)' => \7, 'SCALAR(0x224f60)' => \5, 'db_loop' => \1, 'SCALAR(0x18243b0)' => \9, 'SCALAR(0x224f3c)' => \3 }; What you probably wanted was an array reference. If it was, why not return a reference? sub GetOfficers { return [ 1 .. 9 ]; } I get: $VAR1 = { 'db_loop' => \[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] }; Second, GetOfficers() is called as a subroutine, not as a method of an object. But it is written as an object method. Try this. $template->param( db_loop => $self->GetOfficers() ); Now for some code review. :) : sub mainmenu : { : my $self = shift; : my $dbh = $self->param('mydbh'); : : # Get the CGI query object so that you can use the CGI.pm : # modules. : my $q = $self->query(); This is not needed unless you plan to do something with $q. : : # Setup the template to use for the output. : my $template = $self->load_tmpl('test2.tmpl.htm'); : : # call param to fill in the loop with the loop data by : # reference. : $template->param(db_loop => \GetOfficers()); $template->param( db_loop => $self->GetOfficers() ); : # Output the template... : $template->output; : } : : sub GetOfficers : { : my $self = shift; : : my $dbh = $self->param('mydbh'); : : # Get the CGI query object so that you can use the CGI.pm modules. : my $q = $self->query(); Not needed unless you plan to do something with $q. : my $sqlstr = ''; : : $sqlstr = <<SQLStmt; : SELECT Name FROM qryOfficer; : : SQLStmt What's wrong with this? Why use a HERE doc for such a short assignment? my $sqlstr = 'SELECT Name FROM qryOfficer'; : #SELECT Family FROM tblOfficer; : : # Get the database connection then Prepare and Execute the SQL : Statement my $sth = $dbh->prepare($sqlstr); : $sth->execute() : || die "Could not execute SQL Statement!!\n$sqlstr"; : : # Setup the template to use for the output. : my $template = $self->load_tmpl('test2.tmpl.htm'); No, that's not needed here. The template isn't used in this method. : my @Officer; # the loop data will be put in here for the html : # template. : : # Now get the table rows and send the list off to the HTML form..... : while ( my $row = $sth->fetchrow_hashref ) : { : #set up the data in an array to give to the HTML template.... : my %line = : ( : HTML_ProjectName => $row->{Name}, : ); : # put this row into the loop by reference : push(@Officer, \%line); Wouldn't fetchall_arrayref() be better here? (Not tested.) sub GetOfficers { my $self = shift; my $sql = 'SELECT Name as HTML_ProjectName FROM qryOfficer'; my $dbh = $self->param( 'mydbh' ); my $sth = $dbh->prepare( $sql ); $sth->execute() || die qq(Could not execute SQL: "$sql"); return fetchall_arrayref( { HTML_ProjectName => 1 } ); } : : } : return @Officer return [EMAIL PROTECTED]; : } HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>