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 / Plugins.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-30  |  30.9 KB  |  1,042 lines

  1. #============================================================= -*-Perl-*-
  2. #
  3. # Template::Plugins
  4. #
  5. # DESCRIPTION
  6. #   Plugin provider which handles the loading of plugin modules and 
  7. #   instantiation of plugin objects.
  8. #
  9. # AUTHORS
  10. #   Andy Wardley <abw@kfs.org>
  11. #
  12. # COPYRIGHT
  13. #   Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
  14. #   Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
  15. #
  16. #   This module is free software; you can redistribute it and/or
  17. #   modify it under the same terms as Perl itself.
  18. #
  19. #----------------------------------------------------------------------------
  20. #
  21. # $Id: Plugins.pm,v 2.70 2004/01/13 16:19:15 abw Exp $
  22. #
  23. #============================================================================
  24.  
  25. package Template::Plugins;
  26.  
  27. require 5.004;
  28.  
  29. use strict;
  30. use base qw( Template::Base );
  31. use vars qw( $VERSION $DEBUG $STD_PLUGINS );
  32. use Template::Constants;
  33.  
  34. $VERSION = sprintf("%d.%02d", q$Revision: 2.70 $ =~ /(\d+)\.(\d+)/);
  35.  
  36. $STD_PLUGINS   = {
  37.     'autoformat' => 'Template::Plugin::Autoformat',
  38.     'cgi'        => 'Template::Plugin::CGI',
  39.     'datafile'   => 'Template::Plugin::Datafile',
  40.     'date'       => 'Template::Plugin::Date',
  41.     'debug'      => 'Template::Plugin::Debug',
  42.     'directory'  => 'Template::Plugin::Directory',
  43.     'dbi'        => 'Template::Plugin::DBI',
  44.     'dumper'     => 'Template::Plugin::Dumper',
  45.     'file'       => 'Template::Plugin::File',
  46.     'format'     => 'Template::Plugin::Format',
  47.     'html'       => 'Template::Plugin::HTML',
  48.     'image'      => 'Template::Plugin::Image',
  49.     'iterator'   => 'Template::Plugin::Iterator',
  50.     'pod'        => 'Template::Plugin::Pod',
  51.     'table'      => 'Template::Plugin::Table',
  52.     'url'        => 'Template::Plugin::URL',
  53.     'view'       => 'Template::Plugin::View',
  54.     'wrap'       => 'Template::Plugin::Wrap',
  55.     'xmlstyle'   => 'Template::Plugin::XML::Style',
  56. };
  57.  
  58.  
  59. #========================================================================
  60. #                         -- PUBLIC METHODS --
  61. #========================================================================
  62.  
  63. #------------------------------------------------------------------------
  64. # fetch($name, \@args, $context)
  65. #
  66. # General purpose method for requesting instantiation of a plugin
  67. # object.  The name of the plugin is passed as the first parameter.
  68. # The internal FACTORY lookup table is consulted to retrieve the
  69. # appropriate factory object or class name.  If undefined, the _load()
  70. # method is called to attempt to load the module and return a factory
  71. # class/object which is then cached for subsequent use.  A reference
  72. # to the calling context should be passed as the third parameter.
  73. # This is passed to the _load() class method.  The new() method is
  74. # then called against the factory class name or prototype object to
  75. # instantiate a new plugin object, passing any arguments specified by
  76. # list reference as the second parameter.  e.g. where $factory is the
  77. # class name 'MyClass', the new() method is called as a class method,
  78. # $factory->new(...), equivalent to MyClass->new(...) .  Where
  79. # $factory is a prototype object, the new() method is called as an
  80. # object method, $myobject->new(...).  This latter approach allows
  81. # plugins to act as Singletons, cache shared data, etc.  
  82. #
  83. # Returns a reference to a plugin, (undef, STATUS_DECLINE) to decline
  84. # the request or ($error, STATUS_ERROR) on error.
  85. #------------------------------------------------------------------------
  86.  
  87. sub fetch {
  88.     my ($self, $name, $args, $context) = @_;
  89.     my ($factory, $plugin, $error);
  90.  
  91.     $self->debug("fetch($name, ", 
  92.                  defined $args ? ('[ ', join(', ', @$args), ' ]') : '<no args>', ', ',
  93.                  defined $context ? $context : '<no context>', 
  94.                  ')') if $self->{ DEBUG };
  95.  
  96.     # NOTE:
  97.     # the $context ref gets passed as the first parameter to all regular
  98.     # plugins, but not to those loaded via LOAD_PERL;  to hack around
  99.     # this until we have a better implementation, we pass the $args
  100.     # reference to _load() and let it unshift the first args in the 
  101.     # LOAD_PERL case
  102.  
  103.     $args ||= [ ];
  104.     unshift @$args, $context;
  105.  
  106.     $factory = $self->{ FACTORY }->{ $name } ||= do {
  107.     ($factory, $error) = $self->_load($name, $context);
  108.     return ($factory, $error) if $error;            ## RETURN
  109.     $factory;
  110.     };
  111.  
  112.     # call the new() method on the factory object or class name
  113.     eval {
  114.     if (ref $factory eq 'CODE') {
  115.         defined( $plugin = &$factory(@$args) )
  116.         || die "$name plugin failed\n";
  117.     }
  118.     else {
  119.         defined( $plugin = $factory->new(@$args) )
  120.         || die "$name plugin failed: ", $factory->error(), "\n";
  121.     }
  122.     };
  123.     if ($error = $@) {
  124. #    chomp $error;
  125.     return $self->{ TOLERANT } 
  126.            ? (undef,  Template::Constants::STATUS_DECLINED)
  127.            : ($error, Template::Constants::STATUS_ERROR);
  128.     }
  129.  
  130.     return $plugin;
  131. }
  132.  
  133.  
  134.  
  135. #========================================================================
  136. #                        -- PRIVATE METHODS --
  137. #========================================================================
  138.  
  139. #------------------------------------------------------------------------
  140. # _init(\%config)
  141. #
  142. # Private initialisation method.
  143. #------------------------------------------------------------------------
  144.  
  145. sub _init {
  146.     my ($self, $params) = @_;
  147.     my ($pbase, $plugins, $factory) = 
  148.     @$params{ qw( PLUGIN_BASE PLUGINS PLUGIN_FACTORY ) };
  149.  
  150.     $plugins ||= { };
  151.     if (ref $pbase ne 'ARRAY') {
  152.     $pbase = $pbase ? [ $pbase ] : [ ];
  153.     }
  154.     push(@$pbase, 'Template::Plugin');
  155.  
  156.     $self->{ PLUGIN_BASE } = $pbase;
  157.     $self->{ PLUGINS     } = { %$STD_PLUGINS, %$plugins };
  158.     $self->{ TOLERANT    } = $params->{ TOLERANT }  || 0;
  159.     $self->{ LOAD_PERL   } = $params->{ LOAD_PERL } || 0;
  160.     $self->{ FACTORY     } = $factory || { };
  161.     $self->{ DEBUG       } = ( $params->{ DEBUG } || 0 )
  162.                              & Template::Constants::DEBUG_PLUGINS;
  163.  
  164.     return $self;
  165. }
  166.  
  167.  
  168.  
  169. #------------------------------------------------------------------------
  170. # _load($name, $context)
  171. #
  172. # Private method which attempts to load a plugin module and determine the 
  173. # correct factory name or object by calling the load() class method in
  174. # the loaded module.
  175. #------------------------------------------------------------------------
  176.  
  177. sub _load {
  178.     my ($self, $name, $context) = @_;
  179.     my ($factory, $module, $base, $pkg, $file, $ok, $error);
  180.  
  181.     if ($module = $self->{ PLUGINS }->{ $name }) {
  182.     # plugin module name is explicitly stated in PLUGIN_NAME
  183.     $pkg = $module;
  184.     ($file = $module) =~ s|::|/|g;
  185.     $file =~ s|::|/|g;
  186.     $self->debug("loading $module.pm (PLUGIN_NAME)")
  187.             if $self->{ DEBUG };
  188.     $ok = eval { require "$file.pm" };
  189.     $error = $@;
  190.     }
  191.     else {
  192.     # try each of the PLUGIN_BASE values to build module name
  193.     ($module = $name) =~ s/\./::/g;
  194.  
  195.     foreach $base (@{ $self->{ PLUGIN_BASE } }) {
  196.         $pkg = $base . '::' . $module;
  197.         ($file = $pkg) =~ s|::|/|g;
  198.  
  199.         $self->debug("loading $file.pm (PLUGIN_BASE)")
  200.                 if $self->{ DEBUG };
  201.  
  202.         $ok = eval { require "$file.pm" };
  203.         last unless $@;
  204.     
  205.         $error .= "$@\n" 
  206.         unless ($@ =~ /^Can\'t locate $file\.pm/);
  207.     }
  208.     }
  209.  
  210.     if ($ok) {
  211.     $self->debug("calling $pkg->load()") if $self->{ DEBUG };
  212.  
  213.     $factory = eval { $pkg->load($context) };
  214.     $error   = '';
  215.     if ($@ || ! $factory) {
  216.         $error = $@ || 'load() returned a false value';
  217.     }
  218.     }
  219.     elsif ($self->{ LOAD_PERL }) {
  220.     # fallback - is it a regular Perl module?
  221.     ($file = $module) =~ s|::|/|g;
  222.     eval { require "$file.pm" };
  223.     if ($@) {
  224.         $error = $@;
  225.     }
  226.     else {
  227.         # this is a regular Perl module so the new() constructor
  228.         # isn't expecting a $context reference as the first argument;
  229.         # so we construct a closure which removes it before calling
  230.         # $module->new(@_);
  231.         $factory = sub {
  232.         shift;
  233.         $module->new(@_);
  234.         };
  235.         $error   = '';
  236.     }
  237.     }
  238.  
  239.     if ($factory) {
  240.     $self->debug("$name => $factory") if $self->{ DEBUG };
  241.     return $factory;
  242.     }
  243.     elsif ($error) {
  244.     return $self->{ TOLERANT } 
  245.         ? (undef,  Template::Constants::STATUS_DECLINED) 
  246.         : ($error, Template::Constants::STATUS_ERROR);
  247.     }
  248.     else {
  249.     return (undef, Template::Constants::STATUS_DECLINED);
  250.     }
  251. }
  252.  
  253.  
  254. #------------------------------------------------------------------------
  255. # _dump()
  256. # Debug method which constructs and returns text representing the current
  257. # state of the object.
  258. #------------------------------------------------------------------------
  259.  
  260. sub _dump {
  261.     my $self = shift;
  262.     my $output = "[Template::Plugins] {\n";
  263.     my $format = "    %-16s => %s\n";
  264.     my $key;
  265.  
  266.     foreach $key (qw( TOLERANT LOAD_PERL )) {
  267.     $output .= sprintf($format, $key, $self->{ $key });
  268.     }
  269.  
  270.     local $" = ', ';
  271.     my $fkeys = join(", ", keys %{$self->{ FACTORY }});
  272.     my $plugins = $self->{ PLUGINS };
  273.     $plugins = join('', map { 
  274.     sprintf("    $format", $_, $plugins->{ $_ });
  275.     } keys %$plugins);
  276.     $plugins = "{\n$plugins    }";
  277.     
  278.     $output .= sprintf($format, 'PLUGIN_BASE', "[ @{ $self->{ PLUGIN_BASE } } ]");
  279.     $output .= sprintf($format, 'PLUGINS', $plugins);
  280.     $output .= sprintf($format, 'FACTORY', $fkeys);
  281.     $output .= '}';
  282.     return $output;
  283. }
  284.  
  285.  
  286. 1;
  287.  
  288. __END__
  289.  
  290.  
  291. #------------------------------------------------------------------------
  292. # IMPORTANT NOTE
  293. #   This documentation is generated automatically from source
  294. #   templates.  Any changes you make here may be lost.
  295. #   The 'docsrc' documentation source bundle is available for download
  296. #   from http://www.template-toolkit.org/docs.html and contains all
  297. #   the source templates, XML files, scripts, etc., from which the
  298. #   documentation for the Template Toolkit is built.
  299. #------------------------------------------------------------------------
  300.  
  301. =head1 NAME
  302.  
  303. Template::Plugins - Plugin provider module
  304.  
  305. =head1 SYNOPSIS
  306.  
  307.     use Template::Plugins;
  308.  
  309.     $plugin_provider = Template::Plugins->new(\%options);
  310.  
  311.     ($plugin, $error) = $plugin_provider->fetch($name, @args);
  312.  
  313. =head1 DESCRIPTION
  314.  
  315. The Template::Plugins module defines a provider class which can be used
  316. to load and instantiate Template Toolkit plugin modules.
  317.  
  318. =head1 METHODS
  319.  
  320. =head2 new(\%params) 
  321.  
  322. Constructor method which instantiates and returns a reference to a
  323. Template::Plugins object.  A reference to a hash array of configuration
  324. items may be passed as a parameter.  These are described below.  
  325.  
  326. Note that the Template.pm front-end module creates a Template::Plugins
  327. provider, passing all configuration items.  Thus, the examples shown
  328. below in the form:
  329.  
  330.     $plugprov = Template::Plugins->new({
  331.     PLUGIN_BASE => 'MyTemplate::Plugin',
  332.         LOAD_PERL   => 1,
  333.     ...
  334.     });
  335.  
  336. can also be used via the Template module as:
  337.  
  338.     $ttengine = Template->new({
  339.     PLUGIN_BASE => 'MyTemplate::Plugin',
  340.         LOAD_PERL   => 1,
  341.     ...
  342.     });
  343.  
  344. as well as the more explicit form of:
  345.  
  346.     $plugprov = Template::Plugins->new({
  347.     PLUGIN_BASE => 'MyTemplate::Plugin',
  348.         LOAD_PERL   => 1,
  349.     ...
  350.     });
  351.  
  352.     $ttengine = Template->new({
  353.     LOAD_PLUGINS => [ $plugprov ],
  354.     });
  355.  
  356. =head2 fetch($name, @args)
  357.  
  358. Called to request that a plugin of a given name be provided.  The relevant 
  359. module is first loaded (if necessary) and the load() class method called 
  360. to return the factory class name (usually the same package name) or a 
  361. factory object (a prototype).  The new() method is then called as a 
  362. class or object method against the factory, passing all remaining
  363. parameters.
  364.  
  365. Returns a reference to a new plugin object or ($error, STATUS_ERROR)
  366. on error.  May also return (undef, STATUS_DECLINED) to decline to
  367. serve the request.  If TOLERANT is set then all errors will be
  368. returned as declines.
  369.  
  370. =head1 CONFIGURATION OPTIONS
  371.  
  372. The following list details the configuration options that can be provided
  373. to the Template::Plugins new() constructor.
  374.  
  375. =over 4
  376.  
  377.  
  378.  
  379.  
  380. =item PLUGINS
  381.  
  382. The PLUGINS options can be used to provide a reference to a hash array
  383. that maps plugin names to Perl module names.  A number of standard
  384. plugins are defined (e.g. 'table', 'cgi', 'dbi', etc.) which map to
  385. their corresponding Template::Plugin::* counterparts.  These can be
  386. redefined by values in the PLUGINS hash.
  387.  
  388.     my $plugins = Template::Plugins->new({
  389.     PLUGINS => {
  390.         cgi => 'MyOrg::Template::Plugin::CGI',
  391.             foo => 'MyOrg::Template::Plugin::Foo',
  392.         bar => 'MyOrg::Template::Plugin::Bar',
  393.     },
  394.     });
  395.  
  396. The USE directive is used to create plugin objects and does so by
  397. calling the plugin() method on the current Template::Context object.
  398. If the plugin name is defined in the PLUGINS hash then the
  399. corresponding Perl module is loaded via require().  The context then
  400. calls the load() class method which should return the class name 
  401. (default and general case) or a prototype object against which the 
  402. new() method can be called to instantiate individual plugin objects.
  403.  
  404. If the plugin name is not defined in the PLUGINS hash then the PLUGIN_BASE
  405. and/or LOAD_PERL options come into effect.
  406.  
  407.  
  408.  
  409.  
  410.  
  411. =item PLUGIN_BASE
  412.  
  413. If a plugin is not defined in the PLUGINS hash then the PLUGIN_BASE is used
  414. to attempt to construct a correct Perl module name which can be successfully 
  415. loaded.  
  416.  
  417. The PLUGIN_BASE can be specified as a single value or as a reference
  418. to an array of multiple values.  The default PLUGIN_BASE value,
  419. 'Template::Plugin', is always added the the end of the PLUGIN_BASE
  420. list (a single value is first converted to a list).  Each value should
  421. contain a Perl package name to which the requested plugin name is
  422. appended.
  423.  
  424. example 1:
  425.  
  426.     my $plugins = Template::Plugins->new({
  427.     PLUGIN_BASE => 'MyOrg::Template::Plugin',
  428.     });
  429.  
  430.     [% USE Foo %]    # => MyOrg::Template::Plugin::Foo
  431.                        or        Template::Plugin::Foo 
  432.  
  433. example 2:
  434.  
  435.     my $plugins = Template::Plugins->new({
  436.     PLUGIN_BASE => [   'MyOrg::Template::Plugin',
  437.              'YourOrg::Template::Plugin'  ],
  438.     });
  439.  
  440.     [% USE Foo %]    # =>   MyOrg::Template::Plugin::Foo
  441.                        or YourOrg::Template::Plugin::Foo 
  442.                        or          Template::Plugin::Foo 
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449. =item LOAD_PERL
  450.  
  451. If a plugin cannot be loaded using the PLUGINS or PLUGIN_BASE
  452. approaches then the provider can make a final attempt to load the
  453. module without prepending any prefix to the module path.  This allows
  454. regular Perl modules (i.e. those that don't reside in the
  455. Template::Plugin or some other such namespace) to be loaded and used
  456. as plugins.
  457.  
  458. By default, the LOAD_PERL option is set to 0 and no attempt will be made
  459. to load any Perl modules that aren't named explicitly in the PLUGINS
  460. hash or reside in a package as named by one of the PLUGIN_BASE
  461. components.  
  462.  
  463. Plugins loaded using the PLUGINS or PLUGIN_BASE receive a reference to
  464. the current context object as the first argument to the new()
  465. constructor.  Modules loaded using LOAD_PERL are assumed to not
  466. conform to the plugin interface.  They must provide a new() class
  467. method for instantiating objects but it will not receive a reference
  468. to the context as the first argument.  Plugin modules should provide a
  469. load() class method (or inherit the default one from the
  470. Template::Plugin base class) which is called the first time the plugin
  471. is loaded.  Regular Perl modules need not.  In all other respects,
  472. regular Perl objects and Template Toolkit plugins are identical.
  473.  
  474. If a particular Perl module does not conform to the common, but not
  475. unilateral, new() constructor convention then a simple plugin wrapper
  476. can be written to interface to it.
  477.  
  478.  
  479.  
  480.  
  481. =item TOLERANT
  482.  
  483. The TOLERANT flag is used by the various Template Toolkit provider
  484. modules (Template::Provider, Template::Plugins, Template::Filters) to
  485. control their behaviour when errors are encountered.  By default, any
  486. errors are reported as such, with the request for the particular
  487. resource (template, plugin, filter) being denied and an exception
  488. raised.  When the TOLERANT flag is set to any true values, errors will
  489. be silently ignored and the provider will instead return
  490. STATUS_DECLINED.  This allows a subsequent provider to take
  491. responsibility for providing the resource, rather than failing the
  492. request outright.  If all providers decline to service the request,
  493. either through tolerated failure or a genuine disinclination to
  494. comply, then a 'E<lt>resourceE<gt> not found' exception is raised.
  495.  
  496.  
  497.  
  498.  
  499. =item DEBUG
  500.  
  501. The DEBUG option can be used to enable debugging messages from the
  502. Template::Plugins module by setting it to include the DEBUG_PLUGINS
  503. value.
  504.  
  505.     use Template::Constants qw( :debug );
  506.  
  507.     my $template = Template->new({
  508.     DEBUG => DEBUG_FILTERS | DEBUG_PLUGINS,
  509.     });
  510.  
  511.  
  512.  
  513.  
  514. =back
  515.  
  516.  
  517.  
  518. =head1 TEMPLATE TOOLKIT PLUGINS
  519.  
  520. The following plugin modules are distributed with the Template
  521. Toolkit.  Some of the plugins interface to external modules (detailed
  522. below) which should be downloaded from any CPAN site and installed
  523. before using the plugin.
  524.  
  525. =head2 Autoformat
  526.  
  527. The Autoformat plugin is an interface to Damian Conway's Text::Autoformat 
  528. Perl module which provides advanced text wrapping and formatting.  See
  529. L<Template::Plugin::Autoformat> and L<Text::Autoformat> for further 
  530. details.
  531.  
  532.     [% USE autoformat(left=10, right=20) %]
  533.     [% autoformat(mytext) %]        # call autoformat sub
  534.     [% mytext FILTER autoformat %]  # or use autoformat filter
  535.  
  536. The Text::Autoformat module is available from CPAN:
  537.  
  538.     http://www.cpan.org/modules/by-module/Text/
  539.  
  540. =head2 CGI
  541.  
  542. The CGI plugin is a wrapper around Lincoln Stein's 
  543. E<lt>lstein@genome.wi.mit.eduE<gt> CGI.pm module.  The plugin is 
  544. distributed with the Template Toolkit (see L<Template::Plugin::CGI>)
  545. and the CGI module itself is distributed with recent versions Perl,
  546. or is available from CPAN.
  547.  
  548.     [% USE CGI %]
  549.     [% CGI.param('param_name') %]
  550.     [% CGI.start_form %]
  551.     [% CGI.popup_menu( Name   => 'color', 
  552.                        Values => [ 'Green', 'Brown' ] ) %]
  553.     [% CGI.end_form %]
  554.  
  555. =head2 Datafile
  556.  
  557. Provides an interface to data stored in a plain text file in a simple
  558. delimited format.  The first line in the file specifies field names
  559. which should be delimiter by any non-word character sequence.
  560. Subsequent lines define data using the same delimiter as int he first
  561. line.  Blank lines and comments (lines starting '#') are ignored.  See
  562. L<Template::Plugin::Datafile> for further details.
  563.  
  564. /tmp/mydata:
  565.  
  566.     # define names for each field
  567.     id : email : name : tel
  568.     # here's the data
  569.     fred : fred@here.com : Fred Smith : 555-1234
  570.     bill : bill@here.com : Bill White : 555-5678
  571.  
  572. example:
  573.  
  574.     [% USE userlist = datafile('/tmp/mydata') %]
  575.  
  576.     [% FOREACH user = userlist %]
  577.        [% user.name %] ([% user.id %])
  578.     [% END %]
  579.  
  580. =head2 Date
  581.  
  582. The Date plugin provides an easy way to generate formatted time and date
  583. strings by delegating to the POSIX strftime() routine.   See
  584. L<Template::Plugin::Date> and L<POSIX> for further details.
  585.  
  586.     [% USE date %]
  587.     [% date.format %]        # current time/date
  588.  
  589.     File last modified: [% date.format(template.modtime) %]
  590.  
  591. =head2 Directory
  592.  
  593. The Directory plugin provides a simple interface to a directory and
  594. the files within it.  See L<Template::Plugin::Directory> for further
  595. details.
  596.  
  597.     [% USE dir = Directory('/tmp') %]
  598.     [% FOREACH file = dir.files %]
  599.         # all the plain files in the directory
  600.     [% END %]
  601.     [% FOREACH file = dir.dirs %]
  602.         # all the sub-directories
  603.     [% END %]
  604.  
  605. =head2 DBI
  606.  
  607. The DBI plugin, developed by Simon Matthews
  608. E<lt>sam@knowledgepool.comE<gt>, brings the full power of Tim Bunce's
  609. E<lt>Tim.Bunce@ig.co.ukE<gt> database interface module (DBI) to your
  610. templates.  See L<Template::Plugin::DBI> and L<DBI> for further details.
  611.  
  612.     [% USE DBI('dbi:driver:database', 'user', 'pass') %]
  613.  
  614.     [% FOREACH user = DBI.query( 'SELECT * FROM users' ) %]
  615.        [% user.id %] [% user.name %]
  616.     [% END %]
  617.  
  618. The DBI and relevant DBD modules are available from CPAN:
  619.  
  620.   http://www.cpan.org/modules/by-module/DBI/
  621.  
  622. =head2 Dumper
  623.  
  624. The Dumper plugin provides an interface to the Data::Dumper module.  See
  625. L<Template::Plugin::Dumper> and L<Data::Dumper> for futher details.
  626.  
  627.     [% USE dumper(indent=0, pad="<br>") %]
  628.     [% dumper.dump(myvar, yourvar) %]
  629.  
  630. =head2 File
  631.  
  632. The File plugin provides a general abstraction for files and can be
  633. used to fetch information about specific files within a filesystem.
  634. See L<Template::Plugin::File> for further details.
  635.  
  636.     [% USE File('/tmp/foo.html') %]
  637.     [% File.name %]     # foo.html
  638.     [% File.dir %]      # /tmp
  639.     [% File.mtime %]    # modification time
  640.  
  641. =head2 Filter
  642.  
  643. This module implements a base class plugin which can be subclassed
  644. to easily create your own modules that define and install new filters.
  645.  
  646.     package MyOrg::Template::Plugin::MyFilter;
  647.  
  648.     use Template::Plugin::Filter;
  649.     use base qw( Template::Plugin::Filter );
  650.  
  651.     sub filter {
  652.     my ($self, $text) = @_;
  653.  
  654.     # ...mungify $text...
  655.  
  656.     return $text;
  657.     }
  658.  
  659.     # now load it...
  660.     [% USE MyFilter %]
  661.  
  662.     # ...and use the returned object as a filter
  663.     [% FILTER $MyFilter %]
  664.       ...
  665.     [% END %]
  666.  
  667. See L<Template::Plugin::Filter> for further details.
  668.  
  669. =head2 Format
  670.  
  671. The Format plugin provides a simple way to format text according to a
  672. printf()-like format.   See L<Template::Plugin::Format> for further 
  673. details.
  674.  
  675.     [% USE bold = format('<b>%s</b>') %]
  676.     [% bold('Hello') %]
  677.  
  678. =head2 GD::Image, GD::Polygon, GD::Constants
  679.  
  680. These plugins provide access to the GD graphics library via Lincoln
  681. D. Stein's GD.pm interface.  These plugins allow PNG, JPEG and other
  682. graphical formats to be generated.
  683.  
  684.     [% FILTER null;
  685.         USE im = GD.Image(100,100);
  686.         # allocate some colors
  687.         black = im.colorAllocate(0,   0, 0);
  688.         red   = im.colorAllocate(255,0,  0);
  689.         blue  = im.colorAllocate(0,  0,  255);
  690.         # Draw a blue oval
  691.         im.arc(50,50,95,75,0,360,blue);
  692.         # And fill it with red
  693.         im.fill(50,50,red);
  694.         # Output image in PNG format
  695.         im.png | stdout(1);
  696.        END;
  697.     -%]
  698.  
  699. See L<Template::Plugin::GD::Image> for further details.
  700.  
  701. =head2 GD::Text, GD::Text::Align, GD::Text::Wrap
  702.  
  703. These plugins provide access to Martien Verbruggen's GD::Text,
  704. GD::Text::Align and GD::Text::Wrap modules. These plugins allow the
  705. layout, alignment and wrapping of text when drawing text in GD images.
  706.  
  707.     [% FILTER null;
  708.         USE gd  = GD.Image(200,400);
  709.         USE gdc = GD.Constants;
  710.         black = gd.colorAllocate(0,   0, 0);
  711.         green = gd.colorAllocate(0, 255, 0);
  712.         txt = "This is some long text. " | repeat(10);
  713.         USE wrapbox = GD.Text.Wrap(gd,
  714.          line_space  => 4,
  715.          color       => green,
  716.          text        => txt,
  717.         );
  718.         wrapbox.set_font(gdc.gdMediumBoldFont);
  719.         wrapbox.set(align => 'center', width => 160);
  720.         wrapbox.draw(20, 20);
  721.         gd.png | stdout(1);
  722.       END;
  723.     -%]
  724.  
  725. See L<Template::Plugin::GD::Text>, L<Template::Plugin::GD::Text::Align>
  726. and L<Template::Plugin::GD::Text::Wrap> for further details.
  727.  
  728. =head2 GD::Graph::lines, GD::Graph::bars, GD::Graph::points, GD::Graph::linespoin
  729. ts, GD::Graph::area, GD::Graph::mixed, GD::Graph::pie
  730.  
  731. These plugins provide access to Martien Verbruggen's GD::Graph module
  732. that allows graphs, plots and charts to be created. These plugins allow
  733. graphs, plots and charts to be generated in PNG, JPEG and other
  734. graphical formats.
  735.  
  736.     [% FILTER null;
  737.         data = [
  738.             ["1st","2nd","3rd","4th","5th","6th"],
  739.             [    4,    2,    3,    4,    3,  3.5]
  740.         ];
  741.         USE my_graph = GD.Graph.pie(250, 200);
  742.         my_graph.set(
  743.                 title => 'A Pie Chart',
  744.                 label => 'Label',
  745.                 axislabelclr => 'black',
  746.                 pie_height => 36,
  747.                 transparent => 0,
  748.         );
  749.         my_graph.plot(data).png | stdout(1);
  750.       END;
  751.     -%]
  752.  
  753. See
  754. L<Template::Plugin::GD::Graph::lines>,
  755. L<Template::Plugin::GD::Graph::bars>,
  756. L<Template::Plugin::GD::Graph::points>,
  757. L<Template::Plugin::GD::Graph::linespoints>,
  758. L<Template::Plugin::GD::Graph::area>,
  759. L<Template::Plugin::GD::Graph::mixed>,
  760. L<Template::Plugin::GD::Graph::pie>, and
  761. L<GD::Graph>,
  762. for more details.
  763.  
  764. =head2 GD::Graph::bars3d, GD::Graph::lines3d, GD::Graph::pie3d
  765.  
  766. These plugins provide access to Jeremy Wadsack's GD::Graph3d
  767. module.  This allows 3D bar charts and 3D lines plots to
  768. be generated.
  769.  
  770.     [% FILTER null;
  771.         data = [
  772.             ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
  773.             [    1,    2,    5,    6,    3,  1.5,    1,     3,     4],
  774.         ];
  775.         USE my_graph = GD.Graph.bars3d();
  776.         my_graph.set(
  777.             x_label         => 'X Label',
  778.             y_label         => 'Y label',
  779.             title           => 'A 3d Bar Chart',
  780.             y_max_value     => 8,
  781.             y_tick_number   => 8,
  782.             y_label_skip    => 2,
  783.             # shadows
  784.             bar_spacing     => 8,
  785.             shadow_depth    => 4,
  786.             shadowclr       => 'dred',
  787.             transparent     => 0,
  788.         my_graph.plot(data).png | stdout(1);
  789.       END;
  790.     -%]
  791.  
  792. See
  793. L<Template::Plugin::GD::Graph::lines3d>,
  794. L<Template::Plugin::GD::Graph::bars3d>, and
  795. L<Template::Plugin::GD::Graph::pie3d>
  796. for more details.
  797.  
  798. =head2 HTML
  799.  
  800. The HTML plugin is very new and very basic, implementing a few useful
  801. methods for generating HTML.  It is likely to be extended in the future
  802. or integrated with a larger project to generate HTML elements in a generic
  803. way (as discussed recently on the mod_perl mailing list).
  804.  
  805.     [% USE HTML %]
  806.     [% HTML.escape("if (a < b && c > d) ..." %]
  807.     [% HTML.attributes(border => 1, cellpadding => 2) %]
  808.     [% HTML.element(table => { border => 1, cellpadding => 2 }) %]
  809.  
  810. See L<Template::Plugin::HTML> for further details.
  811.  
  812. =head2 Iterator
  813.  
  814. The Iterator plugin provides a way to create a Template::Iterator
  815. object to iterate over a data set.  An iterator is created
  816. automatically by the FOREACH directive and is aliased to the 'loop'
  817. variable.  This plugin allows an iterator to be explicitly created
  818. with a given name, or the default plugin name, 'iterator'.  See
  819. L<Template::Plugin::Iterator> for further details.
  820.  
  821.     [% USE iterator(list, args) %]
  822.  
  823.     [% FOREACH item = iterator %]
  824.        [% '<ul>' IF iterator.first %]
  825.        <li>[% item %]
  826.        [% '</ul>' IF iterator.last %]
  827.     [% END %]
  828.  
  829. =head2 Pod
  830.  
  831. This plugin provides an interface to the L<Pod::POM|Pod::POM> module
  832. which parses POD documents into an internal object model which can
  833. then be traversed and presented through the Template Toolkit.
  834.  
  835.     [% USE Pod(podfile) %]
  836.  
  837.     [% FOREACH head1 = Pod.head1;
  838.      FOREACH head2 = head1/head2;
  839.        ...
  840.          END;
  841.        END
  842.     %]
  843.  
  844. =head2 String
  845.  
  846. The String plugin implements an object-oriented interface for 
  847. manipulating strings.  See L<Template::Plugin::String> for further 
  848. details.
  849.  
  850.     [% USE String 'Hello' %]
  851.     [% String.append(' World') %]
  852.  
  853.     [% msg = String.new('Another string') %]
  854.     [% msg.replace('string', 'text') %]
  855.  
  856.     The string "[% msg %]" is [% msg.length %] characters long.
  857.  
  858. =head2 Table
  859.  
  860. The Table plugin allows you to format a list of data items into a 
  861. virtual table by specifying a fixed number of rows or columns, with 
  862. an optional overlap.  See L<Template::Plugin::Table> for further 
  863. details.
  864.  
  865.     [% USE table(list, rows=10, overlap=1) %]
  866.  
  867.     [% FOREACH item = table.col(3) %]
  868.        [% item %]
  869.     [% END %]
  870.  
  871. =head2 URL
  872.  
  873. The URL plugin provides a simple way of contructing URLs from a base
  874. part and a variable set of parameters.  See L<Template::Plugin::URL>
  875. for further details.
  876.  
  877.     [% USE mycgi = url('/cgi-bin/bar.pl', debug=1) %]
  878.  
  879.     [% mycgi %]
  880.        # ==> /cgi/bin/bar.pl?debug=1
  881.  
  882.     [% mycgi(mode='submit') %]
  883.        # ==> /cgi/bin/bar.pl?mode=submit&debug=1
  884.  
  885. =head2 Wrap
  886.  
  887. The Wrap plugin uses the Text::Wrap module by David Muir Sharnoff 
  888. E<lt>muir@idiom.comE<gt> (with help from Tim Pierce and many many others)
  889. to provide simple paragraph formatting.  See L<Template::Plugin::Wrap>
  890. and L<Text::Wrap> for further details.
  891.  
  892.     [% USE wrap %]
  893.     [% wrap(mytext, 40, '* ', '  ') %]    # use wrap sub
  894.     [% mytext FILTER wrap(40) -%]    # or wrap FILTER
  895.  
  896. The Text::Wrap module is available from CPAN:
  897.  
  898.     http://www.cpan.org/modules/by-module/Text/
  899.  
  900. =head2 XML::DOM
  901.  
  902. The XML::DOM plugin gives access to the XML Document Object Module via
  903. Clark Cooper E<lt>cooper@sch.ge.comE<gt> and Enno Derksen's 
  904. E<lt>enno@att.comE<gt> XML::DOM module.  See L<Template::Plugin::XML::DOM> 
  905. and L<XML::DOM> for further details.
  906.  
  907.     [% USE dom = XML.DOM %]
  908.     [% doc = dom.parse(filename) %]
  909.  
  910.     [% FOREACH node = doc.getElementsByTagName('CODEBASE') %]
  911.        * [% node.getAttribute('href') %]
  912.     [% END %]
  913.  
  914. The plugin requires the XML::DOM module, available from CPAN:
  915.  
  916.     http://www.cpan.org/modules/by-module/XML/
  917.  
  918. =head2 XML::RSS
  919.  
  920. The XML::RSS plugin is a simple interface to Jonathan Eisenzopf's
  921. E<lt>eisen@pobox.comE<gt> XML::RSS module.  A RSS (Rich Site Summary)
  922. file is typically used to store short news 'headlines' describing
  923. different links within a site.  This plugin allows you to parse RSS
  924. files and format the contents accordingly using templates.  
  925. See L<Template::Plugin::XML::RSS> and L<XML::RSS> for further details.
  926.  
  927.     [% USE news = XML.RSS(filename) %]
  928.    
  929.     [% FOREACH item = news.items %]
  930.        <a href="[% item.link %]">[% item.title %]</a>
  931.     [% END %]
  932.  
  933. The XML::RSS module is available from CPAN:
  934.  
  935.     http://www.cpan.org/modules/by-module/XML/
  936.  
  937. =head2 XML::Simple
  938.  
  939. This plugin implements an interface to the L<XML::Simple|XML::Simple>
  940. module.
  941.  
  942.     [% USE xml = XML.Simple(xml_file_or_text) %]
  943.  
  944.     [% xml.head.title %]
  945.  
  946. See L<Template::Plugin::XML::Simple> for further details.
  947.  
  948. =head2 XML::Style
  949.  
  950. This plugin defines a filter for performing simple stylesheet based 
  951. transformations of XML text.  
  952.  
  953.     [% USE xmlstyle 
  954.            table = { 
  955.                attributes = { 
  956.                    border      = 0
  957.                    cellpadding = 4
  958.                    cellspacing = 1
  959.                }
  960.            }
  961.     %]
  962.  
  963.     [% FILTER xmlstyle %]
  964.     <table>
  965.     <tr>
  966.       <td>Foo</td> <td>Bar</td> <td>Baz</td>
  967.     </tr>
  968.     </table>
  969.     [% END %]
  970.  
  971. See L<Template::Plugin::XML::Style> for further details.
  972.  
  973. =head2 XML::XPath
  974.  
  975. The XML::XPath plugin provides an interface to Matt Sergeant's
  976. E<lt>matt@sergeant.orgE<gt> XML::XPath module.  See 
  977. L<Template::Plugin::XML::XPath> and L<XML::XPath> for further details.
  978.  
  979.     [% USE xpath = XML.XPath(xmlfile) %]
  980.     [% FOREACH page = xpath.findnodes('/html/body/page') %]
  981.        [% page.getAttribute('title') %]
  982.     [% END %]
  983.  
  984. The plugin requires the XML::XPath module, available from CPAN:
  985.  
  986.     http://www.cpan.org/modules/by-module/XML/
  987.  
  988.  
  989.  
  990.  
  991. =head1 BUGS / ISSUES
  992.  
  993. =over 4
  994.  
  995. =item *
  996.  
  997. It might be worthwhile being able to distinguish between absolute
  998. module names and those which should be applied relative to PLUGIN_BASE
  999. directories.  For example, use 'MyNamespace::MyModule' to denote
  1000. absolute module names (e.g. LOAD_PERL), and 'MyNamespace.MyModule' to
  1001. denote relative to PLUGIN_BASE.
  1002.  
  1003. =back
  1004.  
  1005. =head1 AUTHOR
  1006.  
  1007. Andy Wardley E<lt>abw@andywardley.comE<gt>
  1008.  
  1009. L<http://www.andywardley.com/|http://www.andywardley.com/>
  1010.  
  1011.  
  1012.  
  1013.  
  1014. =head1 VERSION
  1015.  
  1016. 2.70, distributed as part of the
  1017. Template Toolkit version 2.13, released on 30 January 2004.
  1018.  
  1019. =head1 COPYRIGHT
  1020.  
  1021.   Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  1022.   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  1023.  
  1024. This module is free software; you can redistribute it and/or
  1025. modify it under the same terms as Perl itself.
  1026.  
  1027. =head1 SEE ALSO
  1028.  
  1029. L<Template|Template>, L<Template::Plugin|Template::Plugin>, L<Template::Context|Template::Context>
  1030.  
  1031. =cut
  1032.  
  1033. # Local Variables:
  1034. # mode: perl
  1035. # perl-indent-level: 4
  1036. # indent-tabs-mode: nil
  1037. # End:
  1038. #
  1039. # vim: expandtab shiftwidth=4:
  1040.