Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Error when passing AoH ref to HTML::Template

by silent11 (Vicar)
on Aug 17, 2005 at 19:01 UTC ( #484533=perlquestion: print w/ replies, xml ) Need Help??
silent11 has asked for the wisdom of the Perl Monks concerning the following question:

I can't see what I'm doing wrong here -- I'm getting an error from the following line:
$template->param( warnings => \@warning ) || die $!;

#!/usr/bin/perl use strict; use HTML::Template; use MIME::Lite; use CGI; use CGI::Carp qw/fatalsToBrowser/; use Data::Dumper; my $c = new CGI; validate( qw/ email phone/ ); my $c = new CGI; print $c->header,$c->start_html('everythings ok'); sub validate { my $c = new CGI; my @params = @_; my @warning; for ( @params ) { push (@warning, { 'missing' => $_ }) unless $c->param($_); } if ( @warning ) { print $c->header; my $template = HTML::Template->new(filename => './templates/warnin +g.html'); $template->param( warnings => \@warning ) || die $!; $template->output() || die $!; print Dumper @warning; exit; } }
Update:

The error

Content-type: text/html Software error: Died at /var/www/cgi-bin/ebranch/assignment/process.cgi line 28. For help, please send mail to the webmaster (root@localhost), giving t +his error message and the time and date of the error.

The Template

<span class="bigText"> Warning! The following fields are missing.< +/span> <TMPL_LOOP warnings> <TMPL_VAR missing><br /> </TMPL_LOOP>


-silent11
Spread Firefox

Comment on Error when passing AoH ref to HTML::Template
Select or Download Code
Replies are listed 'Best First'.
Re: Error when passing AoH ref to HTML::Template
by davidrw (Prior) on Aug 17, 2005 at 19:42 UTC
    Are you sure that $template->param( warnings => \@warning ) is supposed to return a postive value?

    Note that $template->output(); won't actually output anything... per the HTML::Template docs, do print $template->output();
      Are you sure that $template->param( warnings => \@warning ) is supposed to return a postive value?
      It does. When I comment out that line, and just print the output everything works fine. Dumper @warning prints the following:

      $VAR1 = { 'missing' => 'email' }; $VAR2 = { 'missing' => 'phone' };
      You're right. It doesn't. As mentioned below I shouldn't have used die.


      -silent11
      Spread Firefox
Re: Error when passing AoH ref to HTML::Template
by kutsu (Priest) on Aug 17, 2005 at 19:57 UTC

    Firstly you don't need all those my $c =..., but the error is because you sent a hash not a hash reference (also print $template->output).:

    sub validate { my @params = @_; my @warning; for ( @params ) { my %h = ( missing => $_); push (@warning, \%h ) unless $c->param($_); } if ( @warning ) { print $c->header; my $template = HTML::Template->new(filename => './templates/warnin +g.html'); #don't use die $! while testing HTML::Template has it's own error +handling $template->param( warnings => \@warning ); print $template->output(); exit; } }

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      Thanks! I'll give your code a try.

      I didn't want to add the extra instance of CGI, but when I don't add the instance inside of the sub I get the following eror:

      Can't call method "param" on an undefined value at /var/www/cgi-bin/eb +ranch/assignment/process.cgi line 23.
      Any ideas?


      -silent11
      Spread Firefox

        Only create one $c. Then pass it to validate. CGI can get tempermental when you keep creating new ones.

        #!/usr/bin/perl use strict; use HTML::Template; use MIME::Lite; use CGI; use CGI::Carp qw/fatalsToBrowser/; use Data::Dumper; my $c = new CGI; validate($c, qw/ email phone/ ); print $c->header,$c->start_html('everythings ok'); sub validate { my $c = shift; my @params = @_; my @warning; for ( @params ) { push (@warning, { 'missing' => $_ }) unless $c->param($_); } if ( @warning ) { print $c->header; my $template = HTML::Template->new(filename => './templates/warnin +g.html'); $template->param( warnings => \@warning ); print $template->output(); print Dumper @warning; exit; } }

        ___________
        Eric Hodges
Re: Error when passing AoH ref to HTML::Template
by davidrw (Prior) on Aug 17, 2005 at 19:23 UTC
    What is the (exact--copy/paste please) error you're getting? Can you provide the template source as well (in <readmore> tags)?
Re: Error when passing AoH ref to HTML::Template
by punkish (Priest) on Aug 17, 2005 at 19:57 UTC
    You have a couple of errors.

    You are running validate, which is supposed to output the $template, but you are not sending the proper html header until a couple of lines later.

    Update: My bad. I see you are sending $c->header. However, you don't have to re-create your $cgi object. You can simply pass the object to the validate subroutine, and pick it up from there like so.

    Next, $template->output() requires print before it to work.

    --

    when small people start casting long shadows, it is time to go to bed

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (16)
As of 2015-07-28 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls