NAME
Class::AutoPlug::Plugin - base class for a Class::AutoPlug::Pluggable plugin
SYNOPSIS
package Vacuum::Plugin::Screamer;
use base qw(Class::AutoPlug::Plugin);
# Defines a sub that will implement a new foo() method
sub foo_impl :PluggedMethod(foo) {
print "RUNNING!!!!!\n";
}
# Defines a prehook for the bar() method in the Vacuum::Pluggable class.
sub starting :Prehook(bar) {
my ($self, $args) = @_;
print "About to bar ...\n";
# Bypass call to the real method.
# Any other prehooks will still execute.
if ($args eq "skip me") {
print "Asked to skip\n";
return 1;
}
else {
# Go ahead and call the original method.
return 0;
}
}
# Defines a posthook for the (pre-existing) bar() method.
sub stopping :Posthook(bar) {
print "After get...\n";
}
DESCRIPTION
Class::AutoPlug::Plugin
allows you to define methods and hooks using attributes. You don't need to worry about how your code will access the base class; it'll just work.
ATTRIBUTES
This class has no callablel methods; everything's done as attributes. We'll use the local_impl() sub for all our examples.
sub local_impl :PluggedMethod(method_name)
Causes the local_impl() subroutine to be added as the method method_name in the pluggable class. Note that the method_name is a literal bareword!
The parameters it receives are a reference to the pluggable object and whatever other parameters that were specified in the call. You can use last_method() and base_object() to help in processing your method call; you may also store instance data in the pluggable object.
Note that there is no protection from someone else using the same slot names as you in the hash that represents the pluggable object. This allows different plugins to communicate (pro), but can lead to namespace conflicts if two plugins use the same key (con). It's suggested that you add the name of your plugin to your keys to help keep them unique.
sub local_impl :Prehook(base_method_name)
Addes local_impl() as a prehook to the base class's base_method_name method. Again, the name is a literal bareword. You get a reference to the pluggable object and the contents of @_ at the time of the method call.
If you want to skip the method call and handle the call yourself, return 1. If you want processing to go ahead and call the method after doing whatever it is you choose, return 0.
sub local_impl :Posthook(base_method_name)
Exactly like :Prehook, except that the posthook gets the current return value and can alter it as it chooses (or leave it alone). If you choose to alter it, return a list of (1, @new_return_values); if you choose not to, return 0.