Michael Peters wrote: > Rhesa Rozendaal wrote: > This is true. After reading the above, and other comments you make below > this doesn't seem like that big of a change. Okay, here are patches against 4.01 that defines the 'after_*' hooks for the init, prerun, postrun and teardown phases. It doesn't really do much at all except call the 'after_' hook in the appropriate place. It doesn't do anything with load_tmpl since I thought that warranted more discussion. Basically I thought load_tmpl would be most useful if it was given the actual template obj resulting from load_tmpl. -- Michael Peters Developer Plus Three, LP
--- lib/CGI/Application.pm.OLD 2005-06-21 11:43:23.000000000 -0400
+++ lib/CGI/Application.pm 2005-06-21 12:11:54.000000000 -0400
@@ -8,12 +8,17 @@
$CGI::Application::VERSION = '4.01';
my %INSTALLED_CALLBACKS = (
-# hook name package sub
- init => { 'CGI::Application' => [ 'cgiapp_init' ] },
- prerun => { 'CGI::Application' => [ 'cgiapp_prerun' ] },
- postrun => { 'CGI::Application' => [ 'cgiapp_postrun' ] },
- teardown => { 'CGI::Application' => [ 'teardown' ] },
- load_tmpl => { },
+# hook name package sub
+ init => { 'CGI::Application' => [ 'cgiapp_init' ] },
+ after_init => { },
+ prerun => { 'CGI::Application' => [ 'cgiapp_prerun' ] },
+ after_prerun => { },
+ postrun => { 'CGI::Application' => [ 'cgiapp_postrun' ] },
+ after_postrun => { },
+ teardown => { 'CGI::Application' => [ 'teardown' ] },
+ after_teardown => { },
+ load_tmpl => { },
+ after_load_tmpl => { },
);
###################################
@@ -78,6 +83,7 @@
# Pass all constructor args forward. This will allow flexible usage
# down the line.
$self->call_hook('init', @args);
+ $self->call_hook('after_init', @args);
# Call setup() method, which should be implemented in the sub-class!
$self->setup();
@@ -122,6 +128,7 @@
# This hook can be used to provide run mode specific behaviors
# before the run mode actually runs.
$self->call_hook('prerun', $rm);
+ $self->call_hook('after_prerun', $rm);
# Lock prerun_mode from being changed after cgiapp_prerun()
$self->{__PRERUN_MODE_LOCKED} = 1;
@@ -170,6 +177,7 @@
# Call cgiapp_postrun() hook
$self->call_hook('postrun', $bodyref);
+ $self->call_hook('after_postrun', $bodyref);
# Set up HTTP headers
my $headers = $self->_send_headers();
@@ -185,6 +193,7 @@
# clean up operations
$self->call_hook('teardown');
+ $self->call_hook('after_teardown');
return $output;
}
@@ -1873,6 +1882,9 @@
# Object-based: callback will only last for lifetime of this object
$self->add_callback('prerun', \&some_method);
+ # Execute something after a predefined hook
+ $self->add_callback('after_postrun', \&final_output_processing);
+
# If you want to create a new hook location in your application,
# You'll need to know about the following two methods to create
# the hook and call it.
@@ -1894,9 +1906,32 @@
The add_callback method allows you to register a callback
function that is to be called at the given stage of execution.
-Valid hooks include 'init', 'prerun', 'postrun' and 'teardown',
-'load_tmpl', and any other hooks defined using the C<new_hook>
-method.
+The following is a list of predefined hooks:
+
+=over 4
+
+=item init
+
+=item after_init
+
+=item prerun
+
+=item after_prerun
+
+=item postrun
+
+=item after_postrun
+
+=item teardown
+
+=item after_teardown
+
+=item load_tmpl
+
+=back
+
+In addition to the above you may also register a call back for
+any other hooks defined using the C<new_hook> method.
The callback should be a reference to a subroutine or the name of a
method.
--- t/11callbacks.t.OLD 2005-06-14 10:34:42.000000000 -0400
+++ t/11callbacks.t 2005-06-21 12:30:04.000000000 -0400
@@ -40,24 +40,32 @@
# Foo's hooks are added by reference. They cannot be overridden by the
# application
- $caller->add_callback('foo_hook', \&foo_custom);
- $caller->add_callback('init', \&foo_init1);
- $caller->add_callback('init', \&foo_init2);
- $caller->add_callback('prerun', \&foo_prerun);
- $caller->add_callback('postrun', \&foo_postrun);
- $caller->add_callback('teardown', \&foo_teardown);
+ $caller->add_callback('foo_hook', \&foo_custom);
+ $caller->add_callback('init', \&foo_init1);
+ $caller->add_callback('init', \&foo_init2);
+ $caller->add_callback('after_init', \&foo_after_init);
+ $caller->add_callback('prerun', \&foo_prerun);
+ $caller->add_callback('after_prerun', \&foo_after_prerun);
+ $caller->add_callback('postrun', \&foo_postrun);
+ $caller->add_callback('after_postrun', \&foo_after_postrun);
+ $caller->add_callback('teardown', \&foo_teardown);
+ $caller->add_callback('after_teardown', \&foo_after_teardown);
goto &Exporter::import;
}
- sub foo_custom { main::record_event('foo_hook') }
- sub foo_init1 { main::record_event('init') }
- sub foo_init2 { main::record_event('init') }
- sub foo_prerun { main::record_event('prerun') }
- sub foo_postrun { main::record_event('postrun') }
+ sub foo_custom { main::record_event('foo_hook') }
+ sub foo_init1 { main::record_event('init') }
+ sub foo_init2 { main::record_event('init') }
+ sub foo_after_init { main::record_event('after_init') }
+ sub foo_prerun { main::record_event('prerun') }
+ sub foo_after_prerun { main::record_event('after_prerun') }
+ sub foo_postrun { main::record_event('postrun') }
+ sub foo_after_postrun { main::record_event('after_postrun') }
sub foo_teardown {
my $self = shift;
main::record_event('teardown');
$self->call_hook('foo_hook');
}
+ sub foo_after_teardown { main::record_event('after_teardown') }
}
######################################
@@ -214,7 +222,7 @@
Other::Project->add_callback('init', 'other_project_init');
# install an impolite callback that will get run by all CGI::Application apps
- # regardless of whether or not they use My::Project
+ # regardless of whether or not they use Other::Project
CGI::Application->add_callback('init', \&other_project_global_init);
sub other_project_init { main::record_event('init') }
@@ -314,54 +322,63 @@
my @expected_events = (
# init
-
- 'init/CGI::Application::Plugin::Bar::bar_init1', # CAP::Bar
+ 'init/CGI::Application::Plugin::Bar::bar_init1', # CAP::Bar
'bar_hook/CGI::Application::Plugin::Bar::bar_custom',
'init/CGI::Application::Plugin::Bar::bar_init2',
- 'init/CGI::Application::Plugin::Foo::foo_init1', # CAP::Foo
+ 'init/CGI::Application::Plugin::Foo::foo_init1', # CAP::Foo
'init/CGI::Application::Plugin::Foo::foo_init2',
- 'init/My::Project::my_project_init', # My::Project
+ 'init/My::Project::my_project_init', # My::Project
- 'init/My::App::cgiapp_init', # My::App (but installed via CGI::Application)
+ 'init/My::App::cgiapp_init', # My::App (but installed via CGI::Application)
- 'init/My::Project::my_project_global_init', # My::Project (rudely) registered a callback in the
- # CGI::Application class
+ 'init/My::Project::my_project_global_init', # My::Project (rudely) registered a callback in the
+ # CGI::Application class
- 'init/Other::Project::other_project_global_init', # Other::Project (rudely) registered a callback in the
- # CGI::Application class, which forces us to run it
+ 'init/Other::Project::other_project_global_init', # Other::Project (rudely) registered a callback in the
+ # CGI::Application class, which forces us to run it
+ # after_init
+ 'after_init/CGI::Application::Plugin::Foo::foo_after_init', # CAP::Foo
# prerun
+ 'prerun/My::App::my_app_obj_prerun', # My::App (installed in object)
- 'prerun/My::App::my_app_obj_prerun', # My::App (installed in object)
+ 'prerun/CGI::Application::Plugin::Bar::bar_prerun', # CAP::Bar
- 'prerun/CGI::Application::Plugin::Bar::bar_prerun', # CAP::Foo
+ 'prerun/My::App::my_app_class_prerun', # My::App (but installed at runtime)
- 'prerun/My::App::my_app_class_prerun', # My::App (but installed at runtime)
+ 'prerun/CGI::Application::Plugin::Foo::foo_prerun', # CAP::Foo
- 'prerun/CGI::Application::Plugin::Foo::foo_prerun', # CAP::Bar
+ 'prerun/My::App::cgiapp_prerun', # My::App (but installed via CGI::Application)
- 'prerun/My::App::cgiapp_prerun', # My::App (but installed via CGI::Application)
+ # after_prerun
+ 'after_prerun/CGI::Application::Plugin::Foo::foo_after_prerun', # CAP::Foo
# Run mode
- 'runmode/My::App::begin', # My::App
+ 'runmode/My::App::begin', # My::App
# postrun
- 'postrun/CGI::Application::Plugin::Bar::bar_postrun', # CAP::Bar
- 'postrun/CGI::Application::Plugin::Foo::foo_postrun', # CAP::Foo
- 'postrun/My::App::cgiapp_postrun', # My::App (but installed via CGI::Application)
+ 'postrun/CGI::Application::Plugin::Bar::bar_postrun', # CAP::Bar
+ 'postrun/CGI::Application::Plugin::Foo::foo_postrun', # CAP::Foo
+ 'postrun/My::App::cgiapp_postrun', # My::App (but installed via CGI::Application)
+
+ # after_postrun
+ 'after_postrun/CGI::Application::Plugin::Foo::foo_after_postrun', # CAP::Foo
# teardown
- 'teardown/My::App::my_app_teardown', # My::App (but installed in object)
+ 'teardown/My::App::my_app_teardown', # My::App (but installed in object)
- 'teardown/CGI::Application::Plugin::Bar::bar_teardown', # CAP::Bar
- 'teardown/CGI::Application::Plugin::Foo::foo_teardown', # CAP::Foo
- 'foo_hook/CGI::Application::Plugin::Foo::foo_custom', # CAP::Foo
- 'teardown/My::App::teardown', # My::App (but installed via CGI::Application)
+ 'teardown/CGI::Application::Plugin::Bar::bar_teardown', # CAP::Bar
+ 'teardown/CGI::Application::Plugin::Foo::foo_teardown', # CAP::Foo
+ 'foo_hook/CGI::Application::Plugin::Foo::foo_custom',
+ 'teardown/My::App::teardown', # My::App (but installed via CGI::Application)
+
+ # after_teardown
+ 'after_teardown/CGI::Application::Plugin::Foo::foo_after_teardown', # CAP::Foo
);
@@ -400,6 +417,9 @@
'init/Other::Project::other_project_global_init', # Other::Project (rudely) registered a callback in the
# CGI::Application class, which forces us to run it
+ # CGI::Application class, which forces us to run it
+ # after_init
+ 'after_init/CGI::Application::Plugin::Foo::foo_after_init', # CAP::Foo
# prerun
@@ -414,6 +434,8 @@
'prerun/My::App::cgiapp_prerun', # My::App (but installed via CGI::Application)
+ # after_prerun
+ 'after_prerun/CGI::Application::Plugin::Foo::foo_after_prerun', # CAP::Foo
# Run mode
'runmode/My::App::begin', # My::App
@@ -423,6 +445,9 @@
'postrun/CGI::Application::Plugin::Foo::foo_postrun', # CAP::Foo
'postrun/My::App::cgiapp_postrun', # My::App (but installed via CGI::Application)
+ # after_postrun
+ 'after_postrun/CGI::Application::Plugin::Foo::foo_after_postrun', # CAP::Foo
+
# teardown
'teardown/My::App::my_app_teardown', # My::App (but installed in object)
@@ -431,6 +456,9 @@
'foo_hook/CGI::Application::Plugin::Foo::foo_custom', # CAP::Foo
'teardown/My::App::teardown', # My::App (but installed via CGI::Application)
+ # after_teardown
+ 'after_teardown/CGI::Application::Plugin::Foo::foo_after_teardown', # CAP::Foo
+
);
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/suppressed/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed
Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.