Hi All,

It's been fairly quiet on the development side of things with CGI::Application, so I thought I would try to stir things up again ;)

A little while ago, I noticed some talk on the Class::DBI list about a plugin architecture for Class::DBI. Someone actually followed through on the discussion and built Class::DBI::Plugin, which according to the docs is an "Abstract base class for Class::DBI plugins".

The implementation is very simple, and I think is worthwhile looking at for CGI::Application. So I took about an hour out of my usually busy TV watching schedule and threw together CGI::Application::Plugin.

It simplifies and standardizes the way a plugin can be built for CGI::Application, and it works completely outside of the CGI::Application codebase (ie no patches necesary). It avoids the annoyances of building a big inheritance tree for the plugins you want to use by copying a given set of methods into the callers namespace. No exporting or name space munging needs to be done by the plugin modules, as this is done automatically.

As an example of how it works, I have put up new versions of the CGI::Application::Session and CGI::Application::TT modules on my website (http://cees.crtconsulting.ca/perl/modules/), as well as a tarball containing the actual CGI::Application::Plugin module. I won't upload this stuff to CPAN unless there is some positive discussion about this, hence it is just on my website for now. If people don't like it, I can just scrap it without polluting the CPAN, no harm done.

I have attached the pm file for those that don't want to pull down the tarball. I've also inlined it, since I have had trouble sending attachments to this list in the past.

Cheers,

Cees

ps this is all experimental stuff, so don't go loading it onto your production sites just yet...



package CGI::Application::Plugin;

use 5.006;
use strict;
use attributes ();

our $VERSION = 0.01;

# code originally pulled from Jean-Christophe Zeus (Class::DBI::Plugin)
our %remember;
sub MODIFY_CODE_ATTRIBUTES { $remember{ $_[1] } = $_[2]; () }
sub FETCH_CODE_ATTRIBUTES  { $remember{ $_[1] } }

sub import
{
my $class = shift;
my $caller = caller;
no strict 'refs';
for my $symname ( keys %{ "$class\::" } ) {
local *sym = ${ "$class\::" }{ $symname };
next unless defined &sym; # We're only in it for the subroutines
*{ "$caller\::$symname" } = \&sym
if grep { defined( $_ ) and $_ eq 'CGIAppPluginMethod' } attributes::get( \&sym );
}
}


1;
__END__

=head1 NAME

CGI::Application::Plugin - Abstract base class for CGI::Application plugins

=head1 SYNOPSIS

  package CGI::Application::Plugin::MyPlugin

  use base 'CGI::Application::Plugin';

  sub method_name : CGIAppPluginMethod {
    my $self = shift;
    # This method is exported to the plugged-in module
  }

  sub this_method_is_not_exported {}

=head1 DESCRIPTION

CGI::Application::Plugin is an abstract base class for CGI::Application
plugins. Its purpose is to make writing plugins easier. Writers of
plugins should be able to concentrate on the functionality their module
provides, instead of having to deal with the symbol table hackery
involved when writing a plugin module.  The only thing that must be
remembered is that all methods that are to be exported are given the
"CGIAppPluginMethod" attribute. All other methods are not exported to
the class using the plugin.

=head1 CAVEATS

So far this module only "sees" methods in the plugin module itself. If there
is a class between the base class and the plugin class in the inheritance
hierarchy, methods of this class will not be found. In other words, inherited
methods will not be found.


=head1 SEE ALSO

=over

=item *

CGI::Application

=back

=head1 AUTHOR

Packaged up for CGI::Application by Cees Hek, E<lt>[EMAIL PROTECTED]<gt>,
but really written by Jean-Christophe Zeus in Class::DBI::Plugin.


=head1 COPYRIGHT AND LICENSE

Copyright (C) 2004 by Cees Hek
Parts are Copyright (C) 2004 by Jean-Christophe Zeus

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

=cut

package CGI::Application::Plugin;

use 5.006;
use strict;
use attributes ();

our $VERSION = 0.01;

# code originally pulled from Jean-Christophe Zeus (Class::DBI::Plugin)
our %remember;
sub MODIFY_CODE_ATTRIBUTES { $remember{ $_[1] } = $_[2]; () }
sub FETCH_CODE_ATTRIBUTES  { $remember{ $_[1] } }

sub import
{
    my $class  = shift;
    my $caller = caller;
    no strict 'refs';
    for my $symname ( keys %{ "$class\::" } ) {
        local *sym = ${ "$class\::" }{ $symname };
        next unless defined &sym; # We're only in it for the subroutines
        *{ "$caller\::$symname" } = \&sym
            if grep { defined( $_ ) and $_ eq 'CGIAppPluginMethod' } attributes::get( 
\&sym );
    }
}

1;
__END__

=head1 NAME

CGI::Application::Plugin - Abstract base class for CGI::Application plugins

=head1 SYNOPSIS

  package CGI::Application::Plugin::MyPlugin

  use base 'CGI::Application::Plugin';

  sub method_name : CGIAppPluginMethod {
    my $self = shift;
    # This method is exported to the plugged-in module
  }

  sub this_method_is_not_exported {}

=head1 DESCRIPTION

CGI::Application::Plugin is an abstract base class for CGI::Application
plugins. Its purpose is to make writing plugins easier. Writers of
plugins should be able to concentrate on the functionality their module
provides, instead of having to deal with the symbol table hackery
involved when writing a plugin module.  The only thing that must be
remembered is that all methods that are to be exported are given the
"CGIAppPluginMethod" attribute. All other methods are not exported to
the class using the plugin.

=head1 CAVEATS

So far this module only "sees" methods in the plugin module itself. If there
is a class between the base class and the plugin class in the inheritance
hierarchy, methods of this class will not be found. In other words, inherited
methods will not be found.

=head1 SEE ALSO

=over

=item *

CGI::Application

=back

=head1 AUTHOR

Packaged up for CGI::Application by Cees Hek, E<lt>[EMAIL PROTECTED]<gt>, 
but really written by Jean-Christophe Zeus in Class::DBI::Plugin.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2004 by Cees Hek
Parts are Copyright (C) 2004 by Jean-Christophe Zeus

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

=cut


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[EMAIL PROTECTED]/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to