Subject: | New CHAIN method attribute |
I was wondering what you thought about an addition to Class::Std.
The CUMULATIVE tag allows you to get the results from every method in the inheritance tree, as if you had called all of them individually. I propose a CHAIN tag that performs similarly, but instead it should pass the results of one method call into the next, forming sort of a pipeline of methods.
A common pattern is to use NEXT like so:
sub my_method {
my ($self, @args) = @_;
# do something with @args
return $self->NEXT::my_method(@args);
}
In the above case my_method may delegate to another "parent's" my_method all the way up the chain.. the results of the parent's call are passed down to the child in a chain until the final results are returned to the caller.
I'd like something that performs the same basic task, but like this:
sub my_method : CHAIN {
my ($self, @args) = @_;
# do something with @args
return @args;
}
This would call all the methods bottom-up, and CHAIN(BASE FIRST) would go bottom-up the same way CUMULATIVE(BASE FIRST) does. This would replace the other common NEXT pattern:
sub my_method {
my ($self, @args) = @_;
@args = $self->NEXT::my_method(@args);
# do something with @args
return @args;
}
With:
sub my_method : CHAIN(BASE FIRST) {
my ($self, @args) = @_;
# do something with @args
return @args;
}
The reason for this is that I see these as two of the most used NEXT patterns, especially in classes that use NEXT heavly -- a prime example being Catalyst, which used NEXT exactly in this manner.
If you don't like the name CHAIN other possible names might be FILTER, PIPELINE or PIPE.
Let me know if you like the idea, as I'd be happy to submit a patch that allows this.