On Mon, 2004-12-20 at 17:03, Mark Stosberg wrote:
> On 2004-12-20, Cees Hek <suppressed> wrote:
> >
> > The problem comes when two developers want to run two different things
> > at the same stage. Using the sub-classing technique works fine when
> > working on a project by yourself, but we are trying to solve a problem
> > for people that want to distribute modules (or plugins) for others to
> > use.
>
> I think it would be useful to show a complete example. As I've thought
> through it more, I think I've re-confused myself about how it would work.
>
> At what stage in the process do callbacks get registered? It seems
> obvious that they must be registered before they called. So when would
> you have a chance to register one that would run in cgiapp_init?
>
> After you have called new(), it's too late.
>
> Would plug-in users be adding code like this to cgiapp_init?
>
> $self->add_callback('prerun', \&callback);
>
> If you have to explicitly add the callbacks to your code (as a
> plugin-user), it seems like it defeats the point, since you could just
> call the callback method directly at that point.
>
> I'm sorry I'm muddled about this-- yesterday I thought I knew exactly
> what I was getting into. :)
Sorry to resurrect such an old thread.
I have been pondering this for a while now and I've come up with an
enhancement to the Plugin interface. I think it would make these
callbacks much more useful.
I've attached a patch that applies to version 4.0_1 and a test
application to show off the way it works.
I look forward to hearing any feedback people have.
--
Tony Fraser
suppressed
Sybaspace Internet Solutions System Administrator
phone: (250) 246-5368 fax: (250) 246-5398
Binary files CGI.old/.Application.pm.swp and CGI/.Application.pm.swp differ
Binary files CGI.old/Application/.Plugin.pm.swp and CGI/Application/.Plugin.pm.swp differ
diff -ruN CGI.old/Application/Plugin.pm CGI/Application/Plugin.pm
--- CGI.old/Application/Plugin.pm 1969-12-31 16:00:00.000000000 -0800
+++ CGI/Application/Plugin.pm 2005-01-12 16:11:23.000000000 -0800
@@ -0,0 +1,26 @@
+# -*perl*-
+
+use strict;
+
+package CGI::Application::Plugin;
+$CGI::Application::Plugin::VERSION = '0.01';
+
+use CGI::Application '4.02';
+require Exporter;
+
+sub import {
+ return if $_[0] eq 'CGI::Application::Plugin'; # We don't need to be importing ourselves
+ CGI::Application->register_module(scalar(caller), @_);
+ goto &Exporter::import;
+}
+
+sub initialize {
+ my ($class, $app) = @_;
+}
+
+sub add_callbacks {
+ my ($class, $app) = @_;
+}
+
+1;
+__END__
diff -ruN CGI.old/Application.pm CGI/Application.pm
--- CGI.old/Application.pm 2005-01-12 14:59:52.000000000 -0800
+++ CGI/Application.pm 2005-01-12 16:14:15.000000000 -0800
@@ -4,7 +4,7 @@
use Carp;
use strict;
-$CGI::Application::VERSION = '4.0_1';
+$CGI::Application::VERSION = '4.02';
our %DEFAULT_HOOKS = (
init => 'cgiapp_init',
@@ -18,6 +18,12 @@
#### INSTANCE SCRIPT METHODS ####
###################################
+sub register_module {
+ my $class = shift;
+ my ($app_class, $module_class) = @_;
+ eval "push suppressed::INSTALLED_MODULES, \"$module_class\"";
+}
+
sub new {
my $class = shift;
my @args = @_;
@@ -44,6 +50,12 @@
$self->new_hook($_) foreach keys %DEFAULT_HOOKS;
$self->add_callback($_ => $DEFAULT_HOOKS{$_}) foreach keys %DEFAULT_HOOKS;
+ my @installed_modules = eval "suppressed::INSTALLED_MODULES";
+ # Give modules a chance to install themselves (possibly calling new_hook())
+ $_->initialize($self) foreach @installed_modules;
+ # Now that all the hooks are installed give modules the chance to register callbacks
+ $_->add_callbacks($self) foreach @installed_modules;
+
# Process optional new() parameters
my $rprops;
if (ref($args[0]) eq 'HASH') {
#!/usr/bin/perl
use lib '.';
use CGI::Application '4.02';
use CGI::Application::Plugin;
package TestModule1;
use base 'CGI::Application::Plugin';
our @EXPORT = qw/ tm1 /;
sub initialize {
my $class = shift;
my $app = shift;
print STDERR "TestModule1 Intializing...\n";
}
sub add_callbacks {
my $class = shift;
my $app = shift;
print STDERR "TestModule1 Adding Callbacks...\n";
$app->add_callback(test_module2 => 'tm1');
}
sub tm1 {
my $self = shift;
print STDERR "tm1(): " . join(' ', @_) . "\n";
}
1;
package TestModule2;
use base 'CGI::Application::Plugin';
our @EXPORT = qw/ tm2 /;
sub initialize {
my $class = shift;
my $app = shift;
print STDERR "TestModule2 Intializing...\n";
$app->new_hook('test_module2');
}
sub add_callbacks {
my $class = shift;
my $app = shift;
print STDERR "TestModule2 Adding Callbacks...\n";
$app->add_callback(prerun => 'tm2');
}
sub tm2 {
my $self = shift;
$self->call_hook('test_module2', 'foobar');
}
1;
package App;
use base 'CGI::Application';
import TestModule1;
import TestModule2;
sub setup {
my $self = shift;
$self->run_modes( {
start => 'start'
} );
}
sub start {
my $self = shift;
return "<HTML><BODY><H2>Start</H2></BODY></HTML>";
}
1;
package main;
my $app = App->new();
$app->run();
---------------------------------------------------------------------
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.