perlquestion
lhoward
I am writing my first application with [cpan://CGI::Prototype]. I am trying to use the "call yourself until the form in right, and once its ok send you over to the you_did_it_right page" approach shown in Merlyn's <a href="http://www.stonehenge.com/merlyn/LinuxMag/col71.html">Introduction to CGI::Prototype (part 2)</a> article. I am getting hung up about how to pass data between slots (either within one package or between different packages).
<p>
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.
<p>
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.
<readmore>
<code>
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;
</code>
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?
<p>
L