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