lhoward has asked for the wisdom of the Perl Monks concerning the following question:
I am doing most of my heavy lifting in the respond slot (is this where I am going wrong?). If there are errors in the form submission, I want to include some helpful text about what the problems were and display it in the template when I redisplay the form. Similarly, I want to do some computations on the submitted data in the respond slot and then use the results of that computation (which may be as simple as a scalar, or could be a more complicated datastructure) in the current (or another) package's template.
The best method that I have found so far is to add slots in app_enter to hold the values I want to pass around. However, I can't help but think there is a better way to approach this issue. I have a simplified example (written from my head, so it my be a little off) that shows this approach.
Am I trying to force CGI::Prototype to work the way I want to work when instead I should be freeing my mind and working with it the way it wants to be used? How should I be going about passing my data around within the application?Package Foo; use base CGI::Prototype; sub app_enter { my $self = shift; $self->reflect->addSlots([qw(result FIELD)] => ''); } sub respond { my $self = shift; if(($self->param('num')) && ($self->param('num')=~/^\d+$/){ $self->result = $param('num')%2 ? 'odd' : 'even'; } return $self; } sub template { my $self = shift; return \ << "END_OF_TEMPLATE" ; [% self.CGI.header %]<html><head></head><body> [% IF self.result %] [% self.param('num') %] is [% self.result %] <br> [% END %] Please enter an integer: [% self.CGI.start_form; self.CGI.textfield('num'); self.CGI.submit; self.CGI.end_form %] </body></html> END_OF_TEMPLATE } 1;
L