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 / Procedural.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-30  |  4.3 KB  |  171 lines

  1. #==============================================================================
  2. # Template::Plugin::Procedural
  3. #
  4. # DESCRIPTION
  5. #
  6. # A Template Plugin to provide a Template Interface to Data::Dumper
  7. #
  8. # AUTHOR
  9. #   Mark Fowler <mark@twoshortplanks.com>
  10. #
  11. # COPYRIGHT
  12. #
  13. #   Copyright (C) 2002 Mark Fowler.  All Rights Reserved
  14. #
  15. #   This module is free software; you can redistribute it and/or
  16. #   modify it under the same terms as Perl itself.
  17. #
  18. #------------------------------------------------------------------------------
  19. #
  20. # $Id: Procedural.pm,v 1.11 2004/01/13 16:20:38 abw Exp $
  21. #==============================================================================
  22.  
  23. package Template::Plugin::Procedural;
  24.  
  25. require 5.004;
  26.  
  27. use strict;
  28.  
  29. use vars qw( $VERSION $DEBUG $AUTOLOAD );
  30. use base qw( Template::Plugin );
  31.  
  32. $VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
  33. $DEBUG   = 0 unless defined $DEBUG;
  34.  
  35. #------------------------------------------------------------------------
  36. # load
  37. #------------------------------------------------------------------------
  38.  
  39. sub load
  40. {
  41.   my ($class, $context) = @_;
  42.  
  43.   # create a proxy namespace that will be used for objects
  44.   my $proxy = "Template::Plugin::" . $class;
  45.  
  46.   # okay, in our proxy create the autoload routine that will
  47.   # call the right method in the real class
  48.   no strict "refs";
  49.   *{ $proxy . "::AUTOLOAD" } =
  50.     sub
  51.     {
  52.       # work out what the method is called
  53.       $AUTOLOAD =~ s!^.*::!!;
  54.  
  55.       print STDERR "Calling '$AUTOLOAD' in '$class'\n"
  56.     if $DEBUG;
  57.  
  58.       # look up the sub for that method (but in a OO way)
  59.       my $uboat = $class->can($AUTOLOAD);
  60.  
  61.       # if it existed call it as a subroutine, not as a method
  62.       if ($uboat)
  63.       {
  64.     shift @_;
  65.     return $uboat->(@_);
  66.       }
  67.  
  68.       print STDERR "Eeek, no such method '$AUTOLOAD'\n"
  69.     if $DEBUG;
  70.  
  71.       return "";
  72.     };
  73.  
  74.   # create a simple new method that simply returns a blessed
  75.   # scalar as the object.
  76.   *{ $proxy . "::new" } =
  77.     sub
  78.     {
  79.       my $this;
  80.       return bless \$this, $_[0];
  81.     };
  82.  
  83.   return $proxy;
  84. }
  85.  
  86. 1;
  87.  
  88. __END__
  89.  
  90.  
  91. #------------------------------------------------------------------------
  92. # IMPORTANT NOTE
  93. #   This documentation is generated automatically from source
  94. #   templates.  Any changes you make here may be lost.
  95. #   The 'docsrc' documentation source bundle is available for download
  96. #   from http://www.template-toolkit.org/docs.html and contains all
  97. #   the source templates, XML files, scripts, etc., from which the
  98. #   documentation for the Template Toolkit is built.
  99. #------------------------------------------------------------------------
  100.  
  101. =head1 NAME
  102.  
  103. Template::Plugin::Procedural - Base class for procedural plugins
  104.  
  105. =head1 SYNOPSIS
  106.  
  107.     package Template::Plugin::LWPSimple;
  108.     use base qw(Template::Plugin::Procedural);
  109.     use LWP::Simple;  # exports 'get'
  110.     1;
  111.  
  112.     [% USE LWPSimple %]
  113.     [% LWPSimple.get("http://www.tt2.org/") %]
  114.  
  115. =head1 DESCRIPTION
  116.  
  117. B<Template::Plugin::Procedural> is a base class for Template Toolkit
  118. plugins that causes defined subroutines to be called directly rather
  119. than as a method.  Essentially this means that subroutines will not
  120. receive the class name or object as its first argument.
  121.  
  122. This is most useful when creating plugins for modules that normally
  123. work by exporting subroutines that do not expect such additional
  124. arguments.
  125.  
  126. Despite the fact that subroutines will not be called in an OO manner,
  127. inheritance still function as normal.  A class that uses
  128. B<Template::Plugin::Procedural> can be subclassed and both subroutines
  129. defined in the subclass and subroutines defined in the original class
  130. will be available to the Template Toolkit and will be called without
  131. the class/object argument.
  132.  
  133. =head1 AUTHOR
  134.  
  135. Mark Fowler E<lt>mark@twoshortplanks.comE<gt>
  136.  
  137. L<http://www.twoshortplanks.com|http://www.twoshortplanks.com>
  138.  
  139.  
  140.  
  141.  
  142. =head1 VERSION
  143.  
  144. 1.11, distributed as part of the
  145. Template Toolkit version 2.13, released on 30 January 2004.
  146.  
  147. =head1 COPYRIGHT
  148.  
  149.  
  150. Copyright (C) 2002 Mark Fowler E<lt>mark@twoshortplanks.comE<gt>
  151.  
  152. This module is free software; you can redistribute it and/or
  153. modify it under the same terms as Perl itself.
  154.  
  155. =head1 SEE ALSO
  156.  
  157. L<Template|Template>, L<Template::Plugin|Template::Plugin>
  158.  
  159. =cut
  160.  
  161. # Local Variables:
  162. # mode: perl
  163. # perl-indent-level: 4
  164. # indent-tabs-mode: nil
  165. # End:
  166. #
  167. # vim: expandtab shiftwidth=4:
  168.