I find it an Annoyance of C::A that while the current state is abstracted with run_mode, there is no similar abstraction for moving between states. I have to manually code buttons, hyperlinks or hard-code the next state in a hidden field.

What I want is to include with the declaration of each run mode, the set of next user-selectable run modes. Then from that list, (nearly) automagically add submit buttons to the page.

I'm in the midst of writing a Web+DB app now (with C::A, H::T and CGI::FormBuilder). I've written a tiny subclass of C::A that addresses this Annoyance--at present only with C::FB in tow.

Here's what I came up with:

### in NextRMS.pm package CGI::Application::NextRMS; # I am module-naming challenged use base 'CGI::Application'; sub nextrms { my $self = shift; my %args = (@_); my %run_modes; my %nextrms = exists $self->{__NEXTRMS} ? %{$self->{__NEXTRMS}} : {}; for my $key (keys %args) { $run_modes{$key} = exists $args{$key}{handler} ? $args{$key}{handler} : $key; #little feature for lazy me $nextrms{$key} = exists $args{$key}{nextrms} ? $args{$key}{nextrms} : []; } $self->run_modes(%run_modes); $self->{__NEXTRMS} = \%nextrms; } 1; ### In "webapp.pm"... package WebApp; # use base 'CGI::Application::NextRMS'; push @ISA, 'CGI::Application::NextRMS'; use CGI::Carp qw/ fatalsToBrowser /; use CGI::FormBuilder; sub setup { my $self = shift; $self->start_mode('List'); $self->mode_param('_submit'); # compat w/ C::FB $self->nextrms( Add => { handler => 'add', nextrms => ['Cancel Add','Save'], }, Save => { handler => 'db_create', nextrms => ['List'], }, 'Cancel Add' => { handler => 'list', nextrms => ['Add'], }, List => { handler => 'list', nextrms => ['Add'], }, ); $self->{form} = CGI::FormBuilder->new( keepextras => 1, method => 'POST', name => 'myform', params => $self->query(), fields => [qw/ Test /], validate => {}, ); } sub main_form { my $self = shift; return "Runmode: " . $self->get_current_runmode() . "<br>Dispatched sub: " . (caller(1))[3] . $self->{form}->render( submit => $self->{__NEXTRMS}->{$self->get_current_runmode +()}, ); } sub list { my $self = shift; return $self->main_form(); # or a list_form() :P } sub add { my $self = shift; return $self->main_form(); } sub db_create { my $self = shift; return $self->main_form(); } 1; ### In "webapp.cgi"... #use WebApp; my $webapp = WebApp->new(); $webapp->run();

I didn't find anything lightweight like this in my searches, if anyone has seen similar, please share!

Ideas for better 'next run mode' abstraction and general methodology are appreciated!

TIA

UPDATE: Change first code comment

--Solo
--
Without precise calculations we could fly right through a star or bounce too close to a supernova and that'd end your trip real quick, wouldn't it?


In reply to CGI::Application next run mode buttons? by Solo

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.