.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" 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 .\" ======================================================================== .\" .IX Title "AutoRunmode 3pm" .TH AutoRunmode 3pm "2021-01-05" "perl v5.32.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::AutoRunmode \- CGI::App plugin to automatically register runmodes .SH "SYNOPSIS" .IX Header "SYNOPSIS" Using subroutine attributes: .PP .Vb 3 \& package MyApp; \& use base \*(AqCGI::Application\*(Aq; \& use CGI::Application::Plugin::AutoRunmode; \& \& sub my_run_mode : StartRunmode { \& # do something here \& } \& \& sub another_run_mode : Runmode { \& # do something else \& } \& \& # you now have two run modes \& # "my_run_mode" and "another_run_mode" \& # "my_run_mode" is the start (default) run mode .Ve .PP Declare that every method in a (delegate) class is a run mode. .PP .Vb 6 \& package MyAppRunmodes; \& # the delegate class \& sub my_run_mode { \& my ($app, $delegate) = @_; \& # do something here \& } \& \& sub another_run_mode { \& # do something else \& } \& \& package MyApp; \& use base \*(AqCGI::Application\*(Aq; \& \& sub setup{ \& my ($self) = @_; \& my $delegate = \*(AqMyAppRunmodes\*(Aq; \& # $delegate can be a class name or an object \& $self\->param(\*(Aq::Plugin::AutoRunmode::delegate\*(Aq => $delegate); \& } \& \& # you now have two run modes \& # "my_run_mode" and "another_run_mode" .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This plugin for CGI::Application provides easy ways to setup run modes. You can just write the method that implement a run mode, you do not have to explicitly register it with CGI::App anymore. .PP There are two approaches: .IP "Declare run modes with subroutine attributes." 4 .IX Item "Declare run modes with subroutine attributes." You can flag methods in your CGI::App subclass with the attribute \*(L"Runmode\*(R" or \*(L"StartRunmode\*(R" (these attributes are case-insensitive) .IP "Declare that every method in a class is a run mode." 4 .IX Item "Declare that every method in a class is a run mode." You can assign a delegate object, all whose methods will become runmodes .Sp You can also mix both approaches. .Sp Delegate runmodes receive two parameters: The first one is the CGI::App instance, followed by the delegate instance or class name. This can be useful if you have delegate objects that contain state. .Sp It is possible to chain multiple delegates by specifying an array reference containing the delegate instances or class names. This chain is checked from left to right and the runmode will be delegated to the first match. .PP It both cases, the resulting runmodes will have the same name as the subroutine that implements them. They are activated by a cgiapp_prerun hook provided by this plugin (if you are using CGI::Application older than version 4, hooks are not available, and you can import a cgiapp_prerun method instead). .SS "\s-1EXPORT\s0" .IX Subsection "EXPORT" This module needs to export some symbols to do its job. .PP First of all, there are the handlers for the Runmode attribute. .PP In addition to that, the cgiapp_prerun hook is installed in your application class. This is not done as an export per se, but the hook installation is still done in the import subroutine. Sound confusing, is confusing, but you do not really need to know what is going on exactly, just keep in mind that in order to let things go on, you have to \*(L"use\*(R" the module with the default exports: .PP .Vb 1 \& use CGI::Application::Plugin::AutoRunmode; .Ve .PP and not .PP .Vb 3 \& use CGI::Application::Plugin::AutoRunmode (); \& # this will disable the Runmode attributes \& # DO NOT DO THIS .Ve .PP You can also explicitly import the cgiapp_prerun method. This will disable the installation of the hook. Basically, you only want to do this if you are using CGI::Application prior to version 4, where hooks are not supported. .PP .Vb 3 \& use CGI::Application::Plugin::AutoRunmode \& qw [ cgiapp_prerun]; \& # do this if you use CGI::Application version 3.x .Ve .SS "How does it work?" .IX Subsection "How does it work?" After CGI::App has determined the name of the run mode to be executed in the normal way, cgiapp_prerun checks if such a run mode exists in the map configured by \f(CW$self\fR\->\fBrun_modes()\fR. .PP If the run mode already exists, it gets executed normally (this module does nothing). This means that you can mix the ways to declare run modes offered by this plugin with the style provided by core CGI::App. .PP If that is not the case, it tries to find a method of the same name in the application class (or its superclasses) that has been flagged as a Runmode. If it finds one, it augments the mapping with a subroutine reference to that method. .PP If that step fails, it looks if a delegate has been defined and searches the methods of that delegate object for one that matches the name of the runmode. .PP The runmode can then be executed by CGI::App as if it had been set up by \f(CW$self\fR\->\fBrun_modes()\fR in the first place. .PP \fIThe run mode called \*(L"start\*(R"\fR .IX Subsection "The run mode called start" .PP Note that because the plugin only gets activated when you call a run mode that is not registered in the usual run mode map, you cannot use it to create a run mode called \f(CW\*(C`start\*(C'\fR. The CGI:App base class always registers a run mode of that name. .SS "Does it still work if I change the run mode in cgiapp_prerun ?" .IX Subsection "Does it still work if I change the run mode in cgiapp_prerun ?" If you have a cgiapp_prerun method and change the run mode there, the installed hook will not be able to catch it (because of the ordering of hooks). .PP So, if you do that, you have to explicitly make this call before returning from cgiapp_prerun: .PP .Vb 1 \& CGI::Application::Plugin::AutoRunmode::cgiapp_prerun($self); .Ve .PP Again, this is only necessary if you change the run mode (to one that needs the auto-detection feature). .PP Also, this kind of code can be used with CGI::App 3.x if you have a cgiapp_prerun. .SS "StartRunmode" .IX Subsection "StartRunmode" The attribute StartRunmode designates that subroutine to be the start (default) run mode. If you use this feature, the \*(L"traditional\*(R" way of setting the start run mode (calling \&\f(CW\*(C`$self\->start_mode(\*(Aqname\*(Aq)\*(C'\fR) is disabled and can no longer be used in this application (including subclasses and instance scripts). .SS "ErrorRunmode" .IX Subsection "ErrorRunmode" The attribute ErrorRunmode designates that subroutine to be the error run mode. If you use this feature, the \*(L"traditional\*(R" way of setting the error run mode (calling \&\f(CW\*(C`$self\->error_mode(\*(Aqname\*(Aq)\*(C'\fR) is disabled and can no longer be used in this application (including subclasses and instance scripts). This feature requires CGI::App of at least version 3.30. .PP Note that this \*(L"error run mode\*(R" is not a run mode that is directly accessible using its name as a query parameter. It will only be dispatched to internally if the original run mode produced an error. This is exactly how plain CGI:App \f(CW\*(C`error_mode\*(C'\fR behaves as well (you could still declare the method to also be a \f(CW\*(C`:Runmode\*(C'\fR ). .SS "A word on security" .IX Subsection "A word on security" The whole idea of this module (to reduce code complexity by automatically mapping a \s-1URL\s0 to a subroutine that gets executed) is a potential security hazard and great care has to be taken so that a remote user cannot run code that you did not intend them to. .PP In order to prevent a carefully crafted \s-1URL\s0 to access code in other packages, this module disallows non-word characters (such as : ) in run mode names. .PP Also, you have to make sure that when using a delegate object, that it (and its superclasses) only contain run modes (and no other subroutines). .PP The following run mode names are disallowed by this module: .PP .Vb 1 \& can isa VERSION AUTOLOAD new DESTROY .Ve .SS "Effect on the run_modes map" .IX Subsection "Effect on the run_modes map" This module only inserts the current run mode into the run_mode map (unless it is already in there). It does not place any other :Runmodes there. As a result of this behaviour, users of AutoRunmode will most likely find the run mode map almost completely empty. This can lead to strange results if you expect a more complete list of possible run modes there. At this time, there is no workaround for this. Feel free to complain to the author if you have a requirement here. .PP It is possible, however, to query the AutoRunmode plugin if an AutoRunmode exists for a given name. .PP .Vb 1 \& my $check = CGI::Application::Plugin::AutoRunmode::is_auto_runmode($self, $name) .Ve .PP This function returns a code ref if such an AutoRunmode exists. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 CGI::Application::Plugin::AutoRunmode::FileDelegate .IP "\(bu" 4 CGI::Application .IP "\(bu" 4 The CGI::App wiki at . .IP "\(bu" 4 CGI::Application::Plugin::ActionDispatch provides an alternative set of attributes that dispatch according to \s-1PATH_INFO.\s0 It is very similar to the mechanism used in the Catalyst framework. .SH "AUTHOR" .IX Header "AUTHOR" Thilo Planz, .SH "SUPPORT" .IX Header "SUPPORT" Please use the request tracker at \s-1CPAN\s0 to report bugs or feature requests: .PP If you want to support the development of this module with money, you can donate using Flattr: .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2004\-2011 by Thilo Planz .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.