.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "CGI::Application::Plugin::Forward 3pm" .TH CGI::Application::Plugin::Forward 3pm "2022-06-09" "perl v5.34.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" CGI::Application::Plugin::Forward \- Pass control from one run mode to another .SH "VERSION" .IX Header "VERSION" Version 1.06 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use base \*(AqCGI::Application\*(Aq; \& use CGI::Application::Plugin::Forward; \& \& sub setup { \& my $self = shift; \& $self\->run_modes([qw( \& start \& second_runmode \& )]); \& } \& sub start { \& my $self = shift; \& return $self\->forward(\*(Aqsecond_runmode\*(Aq); \& } \& sub second_runmode { \& my $self = shift; \& \& my $rm = $self\->get_current_runmode; # \*(Aqsecond_runmode\*(Aq \& \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The forward method passes control to another run mode and returns its output. This is equivalent to calling \f(CW\*(C`$self\->$other_runmode\*(C'\fR, except that CGI::Application's internal value of the current run mode is updated. .PP This means that calling \f(CW\*(C`$self\->get_current_runmode\*(C'\fR after calling \&\f(CW\*(C`forward\*(C'\fR will return the name of the new run mode. This is useful for modules that depend on the name of the current run mode such as CGI::Application::Plugin::AnyTemplate. .PP For example, here's how to pass control to a run mode named \f(CW\*(C`other_action\*(C'\fR from \f(CW\*(C`start\*(C'\fR while updating the value of \f(CW\*(C`current_run_mode\*(C'\fR: .PP .Vb 10 \& sub setup { \& my $self = shift; \& $self\->run_modes({ \& start => \*(Aqstart\*(Aq, \& other_action => \*(Aqother_method\*(Aq, \& }); \& } \& sub start { \& my $self = shift; \& return $self\->forward(\*(Aqother_action\*(Aq); \& } \& sub other_method { \& my $self = shift; \& \& my $rm = $self\->get_current_runmode; # \*(Aqother_action\*(Aq \& } .Ve .PP Note that forward accepts the \fIname\fR of the run mode (in this case \&\fI'other_action'\fR), which might not be the same as the name of the method that handles the run mode (in this case \fI'other_method'\fR) .PP You can still call \f(CW\*(C`$self\->other_method\*(C'\fR directly, but \&\f(CW\*(C`current_run_mode\*(C'\fR will not be updated: .PP .Vb 10 \& sub setup { \& my $self = shift; \& $self\->run_modes({ \& start => \*(Aqstart\*(Aq, \& other_action => \*(Aqother_method\*(Aq, \& }); \& } \& sub start { \& my $self = shift; \& return $self\->other_method; \& } \& sub other_method { \& my $self = shift; \& \& my $rm = $self\->get_current_runmode; # \*(Aqstart\*(Aq \& } .Ve .PP Forward will work with coderef-based runmodes as well: .PP .Vb 10 \& sub setup { \& my $self = shift; \& $self\->run_modes({ \& start => \*(Aqstart\*(Aq, \& anon_action => sub { \& my $self = shift; \& my $rm = $self\->get_current_runmode; # \*(Aqanon_action\*(Aq \& }, \& }); \& } \& sub start { \& my $self = shift; \& return $self\->forward(\*(Aqanon_action\*(Aq); \& } .Ve .SH "FORWARD vs. REDIRECT" .IX Header "FORWARD vs. REDIRECT" Calling \f(CW\*(C`forward\*(C'\fR changes the run mode of your application, but it stays within the same \s-1HTTP\s0 request. .PP To redirect to a new runmode using a completely new web request, you might consider using the \f(CW\*(C`redirect\*(C'\fR method provided by CGI::Application::Plugin::Redirect. .PP The advantage of using an external redirect as opposed to an internal forward is that it provides a 'clean break' between pages. .PP For instance, in a typical \s-1BREAD\s0 application (Browse, Read, Edit, Add, Delete), after the user completes an action, you usually return the user to the Browse list. For instance, when the user adds a new record via a \s-1POST\s0 form, and your app returns them to the list of records. .PP If you use \f(CW\*(C`forward\*(C'\fR, then you are still in the same request as the original \fIadd record\fR. The user might hit \fIreload\fR, expecting to refresh the list of records. But in fact, \fIreload\fR will attempt to repost the \fIadd record\fR form. The user's browser might present a warning about reposting the same data. The browser may refuse to redisplay the page, due for caching reasons. .PP So in this case, it may make more sense to do a fresh \s-1HTTP\s0 redirect back to the Browse list. .SH "METHODS" .IX Header "METHODS" .SS "forward" .IX Subsection "forward" Runs another run mode passing any parameters you supply. Returns the output of the new run mode. .PP .Vb 1 \& return $self\->forward(\*(Aqrun_mode_name\*(Aq, @run_mode_params); .Ve .SH "HOOKS" .IX Header "HOOKS" Before the forwarded run mode is called, the \f(CW\*(C`forward_prerun\*(C'\fR hook is called. You can use this hook to do any prep work that you want to do before any new run mode gains control. .PP This is similar to CGI::Application's built in \f(CW\*(C`cgiapp_prerun\*(C'\fR method, but it is called each time you call forward; not just the when your application starts. .PP .Vb 4 \& sub setup { \& my $self = shift; \& $self\->add_callback(\*(Aqforward_prerun\*(Aq => \e&prepare_rm_stuff); \& } \& \& sub prepare_rm_stuff { \& my $self = shift; \& # do any necessary prep work here.... \& } .Ve .PP Note that your hooked method will only be called when you call forward. If you never call \f(CW\*(C`forward\*(C'\fR, the hook will not be called. In particuar, the hook will not be called for your application's \&\f(CW\*(C`start_mode\*(C'\fR. For that, you still use \f(CW\*(C`cgiapp_prerun\*(C'\fR. .PP If you want to have a method run for every run mode \fIincluding\fR the \&\f(CW\*(C`start_mode\*(C'\fR, then you can call the hook directly from \&\f(CW\*(C`cgiapp_prerun\*(C'\fR. .PP .Vb 8 \& sub setup { \& my $self = shift; \& $self\->add_callback(\*(Aqforward_prerun\*(Aq => \e&prepare_rm_stuff); \& } \& sub cgiapp_prerun { \& my $self = shift; \& $self\->prepare_rm_stuff; \& } \& \& sub prepare_rm_stuff { \& my $self = shift; \& # do any necessary prep work here.... \& } .Ve .PP Alternately, you can hook \f(CW\*(C`cgiapp_prerun\*(C'\fR to the \f(CW\*(C`forward_prerun\*(C'\fR hook: .PP .Vb 8 \& sub setup { \& my $self = shift; \& $self\->add_callback(\*(Aqforward_prerun\*(Aq => \e&cgiapp_prerun); \& } \& sub cgiapp_prerun { \& my $self = shift; \& # do any necessary prep work here.... \& } .Ve .PP This is a less flexible solution, since certain things that can be done in \f(CW\*(C`cgiapp_prerun\*(C'\fR (like setting \f(CW\*(C`prerun_mode\*(C'\fR) won't work when the method is called from the \f(CW\*(C`forward_prerun\*(C'\fR hook. .SH "AUTHOR" .IX Header "AUTHOR" Michael Graham, \f(CW\*(C`\*(C'\fR .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \&\f(CW\*(C`bug\-cgi\-application\-plugin\-forward@rt.cpan.org\*(C'\fR, or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" Thanks to Mark Stosberg for the idea and...well...the implementation as well. .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright 2005 Michael Graham, All Rights Reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.