NAME

CGI::Application::Plugin::AutoRunmode - CGI::App plugin to automatically register runmodes


SYNOPSIS

Using subroutine attributes:

        package MyApp;
        use base 'CGI::Application';
        use CGI::Application::Plugin::AutoRunmode 
                qw [ cgiapp_prerun];
        
        sub my_run_mode : Runmode {
                # do something here
        }
        
        sub another_run_mode : Runmode {
                # do something else
        }
        
        # you now have two run modes
        # "my_run_mode" and "another_run_mode"
   
Using a delegate object
        package MyAppRunmodes;
        # the delegate class
                sub my_run_mode : Runmode {
                                # do something here
                }
            
                sub another_run_mode : Runmode {
                                # do something else
                }
                
        package MyApp;
        use base 'CGI::Application';
        use CGI::Application::Plugin::AutoRunmode 
                qw [ cgiapp_prerun];
           
                sub setup{
                        my ($self) = @_;
                        my $delegate = 'MyAppRunmodes';
                                # $delegate can be a class name or an object
                        $self->param('::Plugin::AutoRunmode::delegate' => $delegate);
                }
        
         # you now have two run modes
         # "my_run_mode" and "another_run_mode"


DESCRIPTION

This plugin for CGI::Application provides easy ways to setup run modes. You can just write the methods that implement a run mode, you do not have to explicitly register it with CGI::App anymore.

There are two approaches: You can flag methods in your CGI::App subclass with the attribute ``Runmode'', or you can assign a delegate object, all whose methods will become runmodes (you can also mix both approaches).

It both cases, the resulting runmodes will have the same name as the subroutine that implements them, and you can use the cgiapp_prerun method provided by this plugin to activate them.

EXPORT

The module can export a cgiapp_prerun, which you should import unless you already have such a method.

        use CGI::Application::Plugin::AutoRunmode 
                qw [ cgiapp_prerun];

If you already have a cgiapp_prerun, you have to invoke the plugin's code from within your method:

        # if you already have your own cgiapp_prerun
        # do this:
        use CGI::Application::Plugin::AutoRunmode;
                # import nothing
        
        sub cgiapp_prerun{
                CGI::Application::Plugin::AutoRunmode::cgiapp_prerun(@_);
                # your code goes here
        }

using the callback interface

You can also use the new callback interface of CGI::Application::Callbacks. By calling the install method, this plugin will install itself as a callback in the LAST position of the PRERUN phase. This setup enables you to provide additional prerun modes, and you can also change the runmode from within these prerun modes.

        package MyApp;
        use base 'CGI::Application::Callbacks';
        use CGI::Application::Plugin::AutoRunmode ();
        
        sub setup{
                my $self = shift;
                install CGI::Application::Plugin::AutoRunmode($self);
        }
        
        sub do_something : Runmode{
                # ....
        }

How does it work?

After CGI::App has determined the name of the runmode to be executed in the normal way, cgiapp_prerun checks if such a runmode exists in the map configured by $self->run_modes().

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.

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.

The runmode can then be executed by CGI::App as if it had been set up by $self->run_modes() in the first place.


SEE ALSO


AUTHOR

Thilo Planz, <thilo@cpan.org>


COPYRIGHT AND LICENSE

Copyright 2004 by Thilo Planz

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.