Template::Plugin::Class - allow calling of class methods on arbitrary classes


Template-Plugin-Class documentation Contained in the Template-Plugin-Class distribution.

Index


Code Index:

NAME

Top

Template::Plugin::Class - allow calling of class methods on arbitrary classes

SYNOPSIS

Top

  [% USE foo = Class('Foo') %]
  [% foo.bar %]

DESCRIPTION

Top

Template::Plugin::Class allows you to call class methods on arbitrary classes. One use for this is in Class::DBI style applications, where you may do somthing like this:

  [% USE cd = Class('Music::CD') %]
  [% FOREACH disc = cd.retrieve_all %]
  [% disc.artist %] - [% disc.title %]
  [% END %]

CAVEATS

Top

You won't be able to directly call AUTOLOAD or DESTROY methods on the remote class. This shouldn't be a huge hardship.

BUGS

Top

Apart from the mentioned caveat, none currently known. If you find any please contact the author.

AUTHOR

Top

Richard Clamp <richardc@unixbeard.net>

COPYRIGHT

Top

SEE ALSO

Top

Template


Template-Plugin-Class documentation Contained in the Template-Plugin-Class distribution.

use strict;
package Template::Plugin::Class;
use base 'Template::Plugin';
use vars qw( $VERSION );
$VERSION = '0.14';

sub new {
    my $class = shift;
    my $context = shift;
    my $arg = shift;

    # stolen from base.pm
    eval "require $arg";
    # Only ignore "Can't locate" errors from our eval require.
    # Other fatal errors (syntax etc) must be reported.
    (my $filename = $arg) =~ s!::!/!g;
    die if $@ && $@ !~ /Can't locate \Q$filename\E\.pm/;
    no strict 'refs';
    unless (%{"$arg\::"}) {
        require Carp;
        Carp::croak("Package \"$arg\" is empty.\n",
                    "\t(Perhaps you need to 'use' the module ",
                    "which defines that package first.)");
    }

    return bless \$arg, 'Template::Plugin::Class::Proxy';
}

package Template::Plugin::Class::Proxy;
use vars qw( $AUTOLOAD );

sub AUTOLOAD {
    my $self = shift;
    my $class = ref $self;
    my ($method) = ($AUTOLOAD =~ /^$class\::(.*)/);
    $$self->$method(@_);
}

sub DESTROY {}

1;
__END__