| 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 | |||
| 19 | package Template::Plugin::Procedural; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use base 'Template::Plugin'; | ||
| 24 | |||
| 25 | our $VERSION = '3.100'; | ||
| 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 | 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 | |||
| 78 | 1; | ||
| 79 | |||
| 80 | __END__ |