Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Passing data around within CGI::Prototype

by lhoward (Vicar)
on Mar 16, 2006 at 16:21 UTC ( #537190=perlquestion: print w/ replies, xml ) Need Help??
lhoward has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on Passing data around within CGI::Prototype
Download Code
Replies are listed 'Best First'.
Re: Passing data around within CGI::Prototype
by cbrandtbuffalo (Deacon) on Mar 16, 2006 at 17:40 UTC
    We do something similar, with a little more structure. We have a parent class that all apps inherit from, and in that class we do something like this:
    __PACKAGE__->reflect->addSlot( tt_data => {}, perl_data => {}, );
    This creates empty data slots that you can use as a sort of data store. In our code, we can then do things like this:
    $self->perl_data->{pass_around} = 'some calculated data'; $self->tt_data->{some_stuff} = $self->get_some_stuff(); if ( $self->perl_data->{pass_around} ){ blah
    This is basically a simple way to create object data that can be accessed from anywhere without having to pass it. We use separate structures for perl_data and tt_data as an internal standard. Data for the template goes in tt_data and internal data is in perl_data.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2015-07-28 18:15 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 (258 votes), past polls