The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
[%#
  # IMPORTANT NOTE
  #   This documentation is generated automatically from source
  #   templates.  Any changes you make here may be lost.
  # 
  #   The 'docsrc' documentation source bundle is available for download
  #   from http://www.template-toolkit.org/docs.html and contains all
  #   the source templates, XML files, scripts, etc., from which the
  #   documentation for the Template Toolkit is built.
-%]
[% META book = 'Modules'
        page = 'Plugin'
%]
[%  WRAPPER toc;
	PROCESS tocitem 
	        title ="SYNOPSIS"
                subs  = [];
	PROCESS tocitem 
	        title ="DESCRIPTION"
                subs  = [];
	PROCESS tocitem 
	        title ="PLUGIN API"
                subs  = [];
	PROCESS tocitem 
	        title ="DEEPER MAGIC"
                subs  = [];
	PROCESS tocitem 
	        title ="Template::Plugin Delegation"
                subs  = [];
	PROCESS tocitem 
	        title ="AUTHOR"
                subs  = [];
	PROCESS tocitem 
	        title ="VERSION"
                subs  = [];
	PROCESS tocitem 
	        title ="COPYRIGHT"
                subs  = [];
	PROCESS tocitem 
	        title ="SEE ALSO"
                subs  = [];
    END
%]
<!-- Pod to HTML conversion by the Template Toolkit version 2 -->
[% WRAPPER section
    title="SYNOPSIS"
-%]<pre>    package MyOrg::Template::Plugin::MyPlugin;
    use base qw( Template::Plugin );
    use Template::Plugin;
    use MyModule;</pre>
<pre>    sub new {
        my $class   = shift;
        my $context = shift;
	bless {
	    ...
	}, $class;
    }</pre>
[%- END %]
[% WRAPPER section
    title="DESCRIPTION"
-%]<p>
A &quot;plugin&quot; for the Template Toolkit is simply a Perl module which 
exists in a known package location (e.g. Template::Plugin::*) and 
conforms to a regular standard, allowing it to be loaded and used 
automatically.
</p>
<p>
The Template::Plugin module defines a base class from which other 
plugin modules can be derived.  A plugin does not have to be derived
from Template::Plugin but should at least conform to its object-oriented
interface.
</p>
<p>
It is recommended that you create plugins in your own package namespace
to avoid conflict with toolkit plugins.  e.g. 
</p>
<pre>    package MyOrg::Template::Plugin::FooBar;</pre>
<p>
Use the PLUGIN_BASE option to specify the namespace that you use.  e.g.
</p>
<pre>    use Template;
    my $template = Template-&gt;new({ 
	PLUGIN_BASE =&gt; 'MyOrg::Template::Plugin',
    });</pre>
[%- END %]
[% WRAPPER section
    title="PLUGIN API"
-%]<p>
The following methods form the basic interface between the Template
Toolkit and plugin modules.
</p>
<ul>
<li><b>load($context)</b><br>
<p>
This method is called by the Template Toolkit when the plugin module
is first loaded.  It is called as a package method and thus implicitly
receives the package name as the first parameter.  A reference to the
Template::Context object loading the plugin is also passed.  The
default behaviour for the load() method is to simply return the class
name.  The calling context then uses this class name to call the new()
package method.
</p>
<pre>    package MyPlugin;</pre>
<pre>    sub load {               # called as MyPlugin-&gt;load($context)
	my ($class, $context) = @_;
	return $class;       # returns 'MyPlugin'
    }</pre>

<li><b>new($context, @params)</b><br>
<p>
This method is called to instantiate a new plugin object for the USE 
directive.  It is called as a package method against the class name 
returned by load().  A reference to the Template::Context object creating
the plugin is passed, along with any additional parameters specified in
the USE directive.
</p>
<pre>    sub new {                # called as MyPlugin-&gt;new($context)
	my ($class, $context, @params) = @_;
	bless {
	    _CONTEXT =&gt; $context,
	}, $class;	     # returns blessed MyPlugin object
    }</pre>

<li><b>error($error)</b><br>
<p>
This method, inherited from the Template::Base module, is used for 
reporting and returning errors.   It can be called as a package method
to set/return the $ERROR package variable, or as an object method to 
set/return the object _ERROR member.  When called with an argument, it
sets the relevant variable and returns undef.  When called without an
argument, it returns the value of the variable.
</p>
<pre>    sub new {
	my ($class, $context, $dsn) = @_;</pre>
<pre>	return $class-&gt;error('No data source specified')
	    unless $dsn;</pre>
<pre>	bless {
	    _DSN =&gt; $dsn,
	}, $class;
    }</pre>
<pre>    ...</pre>
<pre>    my $something = MyModule-&gt;new()
	|| die MyModule-&gt;error(), &quot;\n&quot;;</pre>
<pre>    $something-&gt;do_something()
	|| die $something-&gt;error(), &quot;\n&quot;;</pre>

</ul>
[%- END %]
[% WRAPPER section
    title="DEEPER MAGIC"
-%]<p>
The Template::Context object that handles the loading and use of
plugins calls the new() and error() methods against the package name
returned by the load() method.  In pseudo-code terms, it might look
something like this:
</p>
<pre>    $class  = MyPlugin-&gt;load($context);       # returns 'MyPlugin'</pre>
<pre>    $object = $class-&gt;new($context, @params)  # MyPlugin-&gt;new(...)
	|| die $class-&gt;error();               # MyPlugin-&gt;error()</pre>
<p>
The load() method may alterately return a blessed reference to an
object instance.  In this case, new() and error() are then called as
<i>object</i> methods against that prototype instance.
</p>
<pre>    package YourPlugin;</pre>
<pre>    sub load {
        my ($class, $context) = @_;
	bless {
	    _CONTEXT =&gt; $context,
	}, $class;
    }</pre>
<pre>    sub new {
	my ($self, $context, @params) = @_;
	return $self;
    }</pre>
<p>
In this example, we have implemented a 'Singleton' plugin.  One object 
gets created when load() is called and this simply returns itself for
each call to new().   
</p>
<p>
Another implementation might require individual objects to be created
for every call to new(), but with each object sharing a reference to
some other object to maintain cached data, database handles, etc.
This pseudo-code example demonstrates the principle.
</p>
<pre>    package MyServer;</pre>
<pre>    sub load {
        my ($class, $context) = @_;
	bless {
	    _CONTEXT =&gt; $context,
	    _CACHE   =&gt; { },
	}, $class;
    }</pre>
<pre>    sub new {
	my ($self, $context, @params) = @_;
	MyClient-&gt;new($self, @params);
    }</pre>
<pre>    sub add_to_cache   { ... }</pre>
<pre>    sub get_from_cache { ... }</pre>
<pre>    package MyClient;</pre>
<pre>    sub new {
	my ($class, $server, $blah) = @_;
	bless {
	    _SERVER =&gt; $server,
	    _BLAH   =&gt; $blah,
	}, $class;
    }</pre>
<pre>    sub get {
	my $self = shift;
	$self-&gt;{ _SERVER }-&gt;get_from_cache(@_);
    }</pre>
<pre>    sub put {
	my $self = shift;
	$self-&gt;{ _SERVER }-&gt;add_to_cache(@_);
    }</pre>
<p>
When the plugin is loaded, a MyServer instance is created.  The new() 
method is called against this object which instantiates and returns a 
MyClient object, primed to communicate with the creating MyServer.
</p>
[%- END %]
[% WRAPPER section
    title="Template::Plugin Delegation"
-%]<p>
As of version 2.01, the Template::Plugin module no longer provides an
AUTOLOAD method to delegate to other objects or classes.  This was a
badly designed feature that caused more trouble than good.  You can
easily add your own AUTOLOAD method to perform delegation if you
require this kind of functionality.
</p>
[%- END %]
[% WRAPPER section
    title="AUTHOR"
-%]<p>
Andy Wardley &lt;abw@andywardley.com&gt;
</p>
<p>
[% ttlink('http://www.andywardley.com/', 'http://www.andywardley.com/') -%]
</p>
[%- END %]
[% WRAPPER section
    title="VERSION"
-%]<p>
2.66, distributed as part of the
Template Toolkit version 2.14, released on 04 October 2004.
</p>
[%- END %]
[% WRAPPER section
    title="COPYRIGHT"
-%]<pre>  Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.</pre>
<p>
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
</p>
[%- END %]
[% WRAPPER section
    title="SEE ALSO"
-%]<p>
[% ttlink('Template', 'Template') -%], [% ttlink('Template::Plugins', 'Template::Plugins') -%], [% ttlink('Template::Context', 'Template::Context') -%]
</p>
[%- END %]