Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

HTML::Templates and Array Refs

by wil (Priest)
on Mar 16, 2004 at 12:21 UTC ( #336974=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I'm having a problem with CGI::Application, HTML::Template and array references. I've got the following code in a sub:

sub get_newsletters { # ---------------------------------------------------------------- # my $self = shift; my $dbh = $self->param('dbh'); use Net::POP3; my $pop = Net::POP3->new('mail.foo.bar', Timeout => 60); my $username = "foo"; my $password = "bar"; my (@messages); if ($pop->login($username, $password) > 0) { my $msgnums = $pop->list; # hashref of msgnum => size foreach my $msgnum (keys %$msgnums) { my %row; my $msg = $pop->get($msgnum); $row{id} = $msgnum; $row{email} = @$msg; warn "print $row{id} - @$msg"; # $pop->delete($msgnum); push (@messages,\%row); } } $pop->quit; my $tmpl_path = "view_emails.shtml"; my $tmpl = $self->load_tmpl($tmpl_path, die_on_bad_params => 0); $tmpl->param ( messages => \@messages, ); $self->_set_username_in_tmpl(\$tmpl); return $tmpl->output; }

And the following in a HTML template file:

<TMPL_LOOP NAME="messages"> <p><TMPL_VAR NAME=id></p> <p><TMPL_VAR NAME=email></p> </TMPL_LOOP>

I keep getting the following error message:

[Tue Mar 16 12:11:04 2004] index.pl: Error executing run mode 'get_new +sletters': HTML::Template->output() : fatal error in loop output : HT +ML::Template::param() : attempt to set parameter 'email' with an arra +y ref - parameter is not a TMPL_LOOP! at /usr/local/share/perl/5.8.2/ +HTML/Template.pm line 2905

I'm consfused by this error. I've tried changing @$msg to $msg and the same error appears. I stuck in that warn in the code, and the warn prints the message to stdout perfectly. But for some reason, HTML::Templates chokes.

Any ideas?

- wil

Replies are listed 'Best First'.
Re: HTML::Templates and Array Refs
by matija (Priest) on Mar 16, 2004 at 12:31 UTC
    $pop->get returns a reference to an array containing the message lines.

    Since you need to pass a scalar there, you should try something like join("",@$msg).

    But I warn you, interpolating the whole message into a page like that is not going to look pretty...

      Thank you! That worked.

      And don't worry, I won't be displaying these on a page. I was just doing that for testing purposes. They will eventually be piped into a database.

      - wil
        A tip that might help you solve these problems on your own next time. Use Data::Dumper! I always dump the data structure in question to STDERR first:
        use Data::Dumper; ... warn Dumper \@messages;
        H::T can still leave you befuddled as to why it got the wrong "data type", but 9 times out of 10 Data::Dumper has shown me the light. ;)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2021-04-20 23:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?