Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Trouble with CGI / HTML::Template

by jms53 (Monk)
on Mar 30, 2013 at 16:23 UTC ( [id://1026290]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having some trouble sending the appropriate parameters to HTML::Template.
I have a module fetching the data from a DB, as follows:

EDIT: I have verified that the data exists on the databas, and is in the format I'm expecting

sub get_account { my $acct_num = pop; my $ar = $dbh->selectall_arrayref( 'SELECT * FROM account WHERE acco +unt_number = ?', { Slice => {} }, $acct_num ); my ($account) = @$ar; $account->{transactions} = \{get_transactions($account->{transaction +s_id})}; $account->{owners} = get_owners( $account->{customers_id} ); return $account; } sub get_transactions { my $transactions_id = shift; my $sql = <<'EOSQL'; SELECT s.transaction_date, type.name, s.amount, s.new_balance FROM transactions t JOIN single_transaction s ON t.single_transaction_id = s.id JOIN transaction_type type ON s.transaction_type_id = type.id WHERE t.id = ? EOSQL my $ar = $dbh->selectall_arrayref( $sql, undef, $transactions_id ); my @lines; my $count = 1; my %transac; for ( @$ar ) { my ($key, $type, $amount, $new_balance) = @$_; $key =~ /\A(\d{4})-(\d{2})-(\d{2})/; my $date = "$3\/$2\/$1"; my @line = ($count, $date, $type, $amount, $new_balance); $transac{$key} = \@line; $count++; } return \%transac; }

And the CGI part:

my $template = HTML::Template->new( filename => 'atm_choose.tmpl', die +_on_bad_params => 0 ); my $account_number = param( 'account_number' ); my $account = MyDB->get_account( $account_number ); $template->param( %$account ); print header, $template->output;

I'm having trouble with setting the transactions in $account->{transactions}. When set as a reference to anon hash, I have an ISA problem, when set as an anon hash, HTML Template complains I'm attempting to set the parameter as a scalar.
I attempted de-referencing the hash, but to no avail.

thank you.

UPDATE 31.03.2013
There seem to be far more bugs than expected in the code, I'm going to start this from scratch.

J -

Replies are listed 'Best First'.
Re: Trouble with CGI / HTML::Template
by Corion (Patriarch) on Mar 30, 2013 at 16:30 UTC

    You say that you tested your data, but this line:

    $account->{transactions} = \{get_transactions($account->{transactions_ +id})};

    ... feels suspicious to me. I think you're double-referencing things here, given that get_transactions already returns a hash reference.

    I suggest you compare the Data::Dumper output of your data structures and pay very close attention to whether you get a hash reference or a reference to a hash reference:

    >perl -MData::Dumper -wle "warn Dumper \{ foo => bar }; warn Dumper { +foo => bar }" Unquoted string "bar" may clash with future reserved word at -e line 1 +. Unquoted string "bar" may clash with future reserved word at -e line 1 +. $VAR1 = \{ 'foo' => 'bar' }; $VAR1 = { 'foo' => 'bar' };

    Also, "I have an ISA problem" is not a very good error description. Maybe you want to tell us the exact error message that HTML::Template gives you?

      After attempting to debug all possible dereferencing possibilities, it turns out there are far more bugs with this code than just attempting to return a hash instead of a hash-ref around.

      I'm going to start this from scratch

      J -

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1026290]
Approved by igelkott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found