I am writing my first application with 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 Introduction to CGI::Prototype (part 2) article. I am getting hung up about how to pass data between slots (either within one package or between different packages).

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.

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;
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?

L


In reply to Passing data around within CGI::Prototype by lhoward

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.