Debian Codegraph @

bench/perl/Template/Plugin/Procedural.pm

raw
1 #==============================================================================
2 #
3 # Template::Plugin::Procedural
4 #
5 # DESCRIPTION
6 # A Template Plugin to provide a Template Interface to Data::Dumper
7 #
8 # AUTHOR
9 # Mark Fowler <[email protected]>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2002 Mark Fowler. All Rights Reserved
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 #==============================================================================
18
19 package Template::Plugin::Procedural;
20
21 use strict;
22 use warnings;
23 use base 'Template::Plugin';
24
25 our $VERSION = 1.17;
26 our $DEBUG = 0 unless defined $DEBUG;
27 our $AUTOLOAD;
28
29 #------------------------------------------------------------------------
30 # load
31 #------------------------------------------------------------------------
32
33 sub load {
34 my ($class, $context) = @_;
35
36 # create a proxy namespace that will be used for objects
37 my $proxy = "Template::Plugin::" . $class;
38
39 # okay, in our proxy create the autoload routine that will
40 # call the right method in the real class
41 no strict "refs";
42 *{ $proxy . "::AUTOLOAD" } = sub {
43 # work out what the method is called
44 $AUTOLOAD =~ s!^.*::!!;
45
46 print STDERR "Calling '$AUTOLOAD' in '$class'\n"
47 if $DEBUG;
48
49 # look up the sub for that method (but in a OO way)
50 my $uboat = $class->can($AUTOLOAD);
51
52 # if it existed call it as a subroutine, not as a method
53 if ($uboat) {
54 shift @_;
55 return $uboat->(@_);
56 }
57
58 print STDERR "Eeek, no such method '$AUTOLOAD'\n"
59 if $DEBUG;
60
61 return "";
62 };
63
64 # create a simple new method that simply returns a blessed
65 # scalar as the object.
66 *{ $proxy . "::new" } = sub {
67 my $this;
68 return bless \$this, $_[0];
69 };
70
71 return $proxy;
72 }
73
74 1;
75
76 __END__
77
78 =head1 NAME
79
80 Template::Plugin::Procedural - Base class for procedural plugins
81
82 =head1 SYNOPSIS
83
84 package Template::Plugin::LWPSimple;
85 use base qw(Template::Plugin::Procedural);
86 use LWP::Simple; # exports 'get'
87 1;
88
89 [% USE LWPSimple %]
90 [% LWPSimple.get("http://www.tt2.org/") %]
91
92 =head1 DESCRIPTION
93
94 C<Template::Plugin::Procedural> is a base class for Template Toolkit
95 plugins that causes defined subroutines to be called directly rather
96 than as a method. Essentially this means that subroutines will not
97 receive the class name or object as its first argument.
98
99 This is most useful when creating plugins for modules that normally
100 work by exporting subroutines that do not expect such additional
101 arguments.
102
103 Despite the fact that subroutines will not be called in an OO manner,
104 inheritance still function as normal. A class that uses
105 C<Template::Plugin::Procedural> can be subclassed and both subroutines
106 defined in the subclass and subroutines defined in the original class
107 will be available to the Template Toolkit and will be called without
108 the class/object argument.
109
110 =head1 AUTHOR
111
112 Mark Fowler E<lt>[email protected]<gt> L<http://www.twoshortplanks.com>
113
114 =head1 COPYRIGHT
115
116 Copyright (C) 2002 Mark Fowler E<lt>[email protected]<gt>
117
118 This module is free software; you can redistribute it and/or
119 modify it under the same terms as Perl itself.
120
121 =head1 SEE ALSO
122
123 L<Template>, L<Template::Plugin>
124
125 =cut
126
127 # Local Variables:
128 # mode: perl
129 # perl-indent-level: 4
130 # indent-tabs-mode: nil
131 # End:
132 #
133 # vim: expandtab shiftwidth=4: