home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Plugin.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-30  |  11.2 KB  |  410 lines

  1. #============================================================= -*-Perl-*-
  2. #
  3. # Template::Plugin
  4. #
  5. # DESCRIPTION
  6. #
  7. #   Module defining a base class for a plugin object which can be loaded
  8. #   and instantiated via the USE directive.
  9. #
  10. # AUTHOR
  11. #   Andy Wardley   <abw@kfs.org>
  12. #
  13. # COPYRIGHT
  14. #   Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
  15. #   Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
  16. #
  17. #   This module is free software; you can redistribute it and/or
  18. #   modify it under the same terms as Perl itself.
  19. #
  20. #----------------------------------------------------------------------------
  21. #
  22. # $Id: Plugin.pm,v 2.65 2004/01/13 16:19:15 abw Exp $
  23. #
  24. #============================================================================
  25.  
  26. package Template::Plugin;
  27.  
  28. require 5.004;
  29.  
  30. use strict;
  31. use Template::Base;
  32.  
  33. use vars qw( $VERSION $DEBUG $ERROR $AUTOLOAD );
  34. use base qw( Template::Base );
  35.  
  36. $VERSION = sprintf("%d.%02d", q$Revision: 2.65 $ =~ /(\d+)\.(\d+)/);
  37. $DEBUG   = 0;
  38.  
  39.  
  40. #========================================================================
  41. #                      -----  CLASS METHODS -----
  42. #========================================================================
  43.  
  44. #------------------------------------------------------------------------
  45. # load()
  46. #
  47. # Class method called when the plugin module is first loaded.  It 
  48. # returns the name of a class (by default, its own class) or a prototype
  49. # object which will be used to instantiate new objects.  The new() 
  50. # method is then called against the class name (class method) or 
  51. # prototype object (object method) to create a new instances of the 
  52. # object.
  53. #------------------------------------------------------------------------
  54.  
  55. sub load {
  56.     return $_[0];
  57. }
  58.  
  59.  
  60. #------------------------------------------------------------------------
  61. # new($context, $delegate, @params)
  62. #
  63. # Object constructor which is called by the Template::Context to 
  64. # instantiate a new Plugin object.  This base class constructor is 
  65. # used as a general mechanism to load and delegate to other Perl 
  66. # modules.  The context is passed as the first parameter, followed by
  67. # a reference to a delegate object or the name of the module which 
  68. # should be loaded and instantiated.  Any additional parameters passed 
  69. # to the USE directive are forwarded to the new() constructor.
  70. # A plugin object is returned which has an AUTOLOAD method to delegate 
  71. # requests to the underlying object.
  72. #------------------------------------------------------------------------
  73.  
  74. sub new {
  75.     my $class = shift;
  76.     bless {
  77.     }, $class;
  78. }
  79.  
  80. sub old_new {
  81.     my ($class, $context, $delclass, @params) = @_;
  82.     my ($delegate, $delmod);
  83.  
  84.     return $class->error("no context passed to $class constructor\n")
  85.     unless defined $context;
  86.  
  87.     if (ref $delclass) {
  88.     # $delclass contains a reference to a delegate object
  89.     $delegate = $delclass;
  90.     }
  91.     else {
  92.     # delclass is the name of a module to load and instantiate
  93.     ($delmod = $delclass) =~ s|::|/|g;
  94.  
  95.     eval {
  96.         require "$delmod.pm";
  97.         $delegate = $delclass->new(@params)
  98.         || die "failed to instantiate $delclass object\n";
  99.     };
  100.     return $class->error($@) if $@;
  101.     }
  102.  
  103.     bless {
  104.     _CONTEXT  => $context, 
  105.     _DELEGATE => $delegate,
  106.     _PARAMS   => \@params,
  107.     }, $class;
  108. }
  109.  
  110.  
  111. #------------------------------------------------------------------------
  112. # fail($error)
  113. # Version 1 error reporting function, now replaced by error() inherited
  114. # from Template::Base.  Raises a "deprecated function" warning and then
  115. # calls error().
  116. #------------------------------------------------------------------------
  117.  
  118. sub fail {
  119.     my $class = shift;
  120.     my ($pkg, $file, $line) = caller();
  121.     warn "Template::Plugin::fail() is deprecated at $file line $line.  Please use error()\n";
  122.     $class->error(@_);
  123. }
  124.  
  125.  
  126. #========================================================================
  127. #                      -----  OBJECT METHODS -----
  128. #========================================================================
  129.  
  130. #------------------------------------------------------------------------
  131. # AUTOLOAD
  132. #
  133. # General catch-all method which delegates all calls to the _DELEGATE 
  134. # object.  
  135. #------------------------------------------------------------------------
  136.  
  137. sub OLD_AUTOLOAD {
  138.     my $self     = shift;
  139.     my $method   = $AUTOLOAD;
  140.  
  141.     $method =~ s/.*:://;
  142.     return if $method eq 'DESTROY';
  143.  
  144.     if (ref $self eq 'HASH') {
  145.     my $delegate = $self->{ _DELEGATE } || return;
  146.     return $delegate->$method(@_);
  147.     }
  148.     my ($pkg, $file, $line) = caller();
  149. #    warn "no such '$method' method called on $self at $file line $line\n";
  150.     return undef;
  151. }
  152.  
  153.  
  154. 1;
  155.  
  156. __END__
  157.  
  158.  
  159. #------------------------------------------------------------------------
  160. # IMPORTANT NOTE
  161. #   This documentation is generated automatically from source
  162. #   templates.  Any changes you make here may be lost.
  163. #   The 'docsrc' documentation source bundle is available for download
  164. #   from http://www.template-toolkit.org/docs.html and contains all
  165. #   the source templates, XML files, scripts, etc., from which the
  166. #   documentation for the Template Toolkit is built.
  167. #------------------------------------------------------------------------
  168.  
  169. =head1 NAME
  170.  
  171. Template::Plugin - Base class for Template Toolkit plugins
  172.  
  173. =head1 SYNOPSIS
  174.  
  175.     package MyOrg::Template::Plugin::MyPlugin;
  176.     use base qw( Template::Plugin );
  177.     use Template::Plugin;
  178.     use MyModule;
  179.  
  180.     sub new {
  181.         my $class   = shift;
  182.         my $context = shift;
  183.     bless {
  184.         ...
  185.     }, $class;
  186.     }
  187.  
  188. =head1 DESCRIPTION
  189.  
  190. A "plugin" for the Template Toolkit is simply a Perl module which 
  191. exists in a known package location (e.g. Template::Plugin::*) and 
  192. conforms to a regular standard, allowing it to be loaded and used 
  193. automatically.
  194.  
  195. The Template::Plugin module defines a base class from which other 
  196. plugin modules can be derived.  A plugin does not have to be derived
  197. from Template::Plugin but should at least conform to its object-oriented
  198. interface.
  199.  
  200. It is recommended that you create plugins in your own package namespace
  201. to avoid conflict with toolkit plugins.  e.g. 
  202.  
  203.     package MyOrg::Template::Plugin::FooBar;
  204.  
  205. Use the PLUGIN_BASE option to specify the namespace that you use.  e.g.
  206.  
  207.     use Template;
  208.     my $template = Template->new({ 
  209.     PLUGIN_BASE => 'MyOrg::Template::Plugin',
  210.     });
  211.  
  212. =head1 PLUGIN API
  213.  
  214. The following methods form the basic interface between the Template
  215. Toolkit and plugin modules.
  216.  
  217. =over 4
  218.  
  219. =item load($context)
  220.  
  221. This method is called by the Template Toolkit when the plugin module
  222. is first loaded.  It is called as a package method and thus implicitly
  223. receives the package name as the first parameter.  A reference to the
  224. Template::Context object loading the plugin is also passed.  The
  225. default behaviour for the load() method is to simply return the class
  226. name.  The calling context then uses this class name to call the new()
  227. package method.
  228.  
  229.     package MyPlugin;
  230.  
  231.     sub load {               # called as MyPlugin->load($context)
  232.     my ($class, $context) = @_;
  233.     return $class;       # returns 'MyPlugin'
  234.     }
  235.  
  236. =item new($context, @params)
  237.  
  238. This method is called to instantiate a new plugin object for the USE 
  239. directive.  It is called as a package method against the class name 
  240. returned by load().  A reference to the Template::Context object creating
  241. the plugin is passed, along with any additional parameters specified in
  242. the USE directive.
  243.  
  244.     sub new {                # called as MyPlugin->new($context)
  245.     my ($class, $context, @params) = @_;
  246.     bless {
  247.         _CONTEXT => $context,
  248.     }, $class;         # returns blessed MyPlugin object
  249.     }
  250.  
  251. =item error($error)
  252.  
  253. This method, inherited from the Template::Base module, is used for 
  254. reporting and returning errors.   It can be called as a package method
  255. to set/return the $ERROR package variable, or as an object method to 
  256. set/return the object _ERROR member.  When called with an argument, it
  257. sets the relevant variable and returns undef.  When called without an
  258. argument, it returns the value of the variable.
  259.  
  260.     sub new {
  261.     my ($class, $context, $dsn) = @_;
  262.  
  263.     return $class->error('No data source specified')
  264.         unless $dsn;
  265.  
  266.     bless {
  267.         _DSN => $dsn,
  268.     }, $class;
  269.     }
  270.  
  271.     ...
  272.  
  273.     my $something = MyModule->new()
  274.     || die MyModule->error(), "\n";
  275.  
  276.     $something->do_something()
  277.     || die $something->error(), "\n";
  278.  
  279. =back
  280.  
  281. =head1 DEEPER MAGIC
  282.  
  283. The Template::Context object that handles the loading and use of
  284. plugins calls the new() and error() methods against the package name
  285. returned by the load() method.  In pseudo-code terms, it might look
  286. something like this:
  287.  
  288.     $class  = MyPlugin->load($context);       # returns 'MyPlugin'
  289.  
  290.     $object = $class->new($context, @params)  # MyPlugin->new(...)
  291.     || die $class->error();               # MyPlugin->error()
  292.  
  293. The load() method may alterately return a blessed reference to an
  294. object instance.  In this case, new() and error() are then called as
  295. I<object> methods against that prototype instance.
  296.  
  297.     package YourPlugin;
  298.  
  299.     sub load {
  300.         my ($class, $context) = @_;
  301.     bless {
  302.         _CONTEXT => $context,
  303.     }, $class;
  304.     }
  305.  
  306.     sub new {
  307.     my ($self, $context, @params) = @_;
  308.     return $self;
  309.     }
  310.  
  311. In this example, we have implemented a 'Singleton' plugin.  One object 
  312. gets created when load() is called and this simply returns itself for
  313. each call to new().   
  314.  
  315. Another implementation might require individual objects to be created
  316. for every call to new(), but with each object sharing a reference to
  317. some other object to maintain cached data, database handles, etc.
  318. This pseudo-code example demonstrates the principle.
  319.  
  320.     package MyServer;
  321.  
  322.     sub load {
  323.         my ($class, $context) = @_;
  324.     bless {
  325.         _CONTEXT => $context,
  326.         _CACHE   => { },
  327.     }, $class;
  328.     }
  329.  
  330.     sub new {
  331.     my ($self, $context, @params) = @_;
  332.     MyClient->new($self, @params);
  333.     }
  334.  
  335.     sub add_to_cache   { ... }
  336.  
  337.     sub get_from_cache { ... }
  338.  
  339.  
  340.     package MyClient;
  341.  
  342.     sub new {
  343.     my ($class, $server, $blah) = @_;
  344.     bless {
  345.         _SERVER => $server,
  346.         _BLAH   => $blah,
  347.     }, $class;
  348.     }
  349.  
  350.     sub get {
  351.     my $self = shift;
  352.     $self->{ _SERVER }->get_from_cache(@_);
  353.     }
  354.  
  355.     sub put {
  356.     my $self = shift;
  357.     $self->{ _SERVER }->add_to_cache(@_);
  358.     }
  359.  
  360. When the plugin is loaded, a MyServer instance is created.  The new() 
  361. method is called against this object which instantiates and returns a 
  362. MyClient object, primed to communicate with the creating MyServer.
  363.  
  364. =head1 Template::Plugin Delegation
  365.  
  366. As of version 2.01, the Template::Plugin module no longer provides an
  367. AUTOLOAD method to delegate to other objects or classes.  This was a
  368. badly designed feature that caused more trouble than good.  You can
  369. easily add your own AUTOLOAD method to perform delegation if you
  370. require this kind of functionality.
  371.  
  372. =head1 AUTHOR
  373.  
  374. Andy Wardley E<lt>abw@andywardley.comE<gt>
  375.  
  376. L<http://www.andywardley.com/|http://www.andywardley.com/>
  377.  
  378.  
  379.  
  380.  
  381. =head1 VERSION
  382.  
  383. 2.65, distributed as part of the
  384. Template Toolkit version 2.13, released on 30 January 2004.
  385.  
  386. =head1 COPYRIGHT
  387.  
  388.   Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  389.   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  390.  
  391. This module is free software; you can redistribute it and/or
  392. modify it under the same terms as Perl itself.
  393.  
  394. =head1 SEE ALSO
  395.  
  396. L<Template|Template>, L<Template::Plugins|Template::Plugins>, L<Template::Context|Template::Context>
  397.  
  398. =cut
  399.  
  400. # Local Variables:
  401. # mode: perl
  402. # perl-indent-level: 4
  403. # indent-tabs-mode: nil
  404. # End:
  405. #
  406. # vim: expandtab shiftwidth=4:
  407.