DESCRIPTION

A recent contribution to CPAN is Class::MixinFactory by simonm . Here we show two more ways to do the same thing in addition to the ones that Simon lists in the SEE ALSO section.

One method relies on Leon Brocard's Language::Functional. The other relies on Toby Ovod-Everett's Class::Prototyped


ALTERNATIVE IMPLMENTATIONS

I include here a file, which when run, produces output similar to the output from factory_class.t the Class::MixinFactory distribution.

It shows two new ways of doing this, the Language::Functional approach and the Class::Prototyped approach.

use strict; use Language::Functional ':all'; sub _uc { uc shift } ; sub bold { sprintf "<b>%s</b>", shift } ; sub italics { sprintf "<i>%s</i>", shift } ; my @telescope = (\&_uc, \&bold, \&italics) ; my $o = foldl { my $seed = shift; my $func = shift; $func->($seed) } "hello, world!", \@telescope; warn $o; # begin prototyped version ------------------------------------------- +------- use Class::Prototyped ':EZACCESS'; my $po; $po = Class::Prototyped->new ( uc_m => sub { uc $_[1] }, italics_m => sub { italics $_[1] }, bold_m => sub { bold $_[1] } ); my @agenda = qw(uc_m italics_m); my @agenda2 = qw(bold_m italics_m uc_m); my $ret = foldl { my $seed = shift; my $meth = shift; $po->$meth($seed +) } "hello, world!", \@agenda; warn $ret; $ret = foldl { my $seed = shift; my $meth = shift; $po->$meth($seed) } + "hello, world!", \@agenda2; warn $ret;


EVALUATION

We can compare these two implementations in a number of ways - readability, dynamic utility, object power, and sub power.

Readability

Of the two implementations, the functional implementation is the easiest to read and follow.

Dynamic utility

The prototype implementation appears to be easier to manipulate dynamically. What I mean by easier to manipulate dynamically is that all one need do is specify a list of strings in order to build up a sequence of operations. Strings can come from many places, such as form data, databases, STDIN, etc. With the functional version, you would need to build yourself a dispatch table in order to get similar functionality. The table building is an intrinsic aspect of using Class::Prototyped. It must be done manually for the functional version.

Object power

Prototyped objects are powerful and flexible. They give one the advantage of objects without all the class-building. The functional version will not have the advantages of prototyped objects without creating objects and then references to the subroutines of those objects (I don't even know if that is possible).

Sub power

An advantage of the functional implementation is that you can use normal monadic functions just by passing a reference to them. With the prototyped implementation, you have to create an object wrapper for them.


CONCLUSION

Perl offers a wealth of ways to solve problems. Simon's recent Class::MixinFactory solved a number of problems with the Mixin approach to runtime generation of "agendas for mixing behavior." He also makes mention of a number of other approaches and perhaps should mention the Class::Delegation manpage in addition.

In this article, I have shown two more ways to mix behavior. The actual one to choose for software design remains open based on other aspects of the intended application.


SEE ALSO

  • To download the CPAN-style distro with working source code, visit: here

    2004-12-06 Janitored by Arunbear - added readmore tags, as per Monastery guidelines
    Considered by DaWolf: "Promote to tutorial? Edit = Yes|Keep = No." Vote tally (Keep/Edit/Delete) = 17/20/0.
    Unconsidered by davido: No prevailing consensus in the vote after several days.

    • Comment on alternate approaches to mixing behavior using Language::Functional and Class::Prototyped
    • Download Code

    Replies are listed 'Best First'.
    Re: alternate approaches to mixing behavior using Language::Functional and Class::Prototyped
    by dragonchild (Archbishop) on Dec 06, 2004 at 14:10 UTC
      It is well thought out, well-designed and covers the functionality offered by earlier MVC frameworks with powerful, featureful controllers such as CGI::Application and Zanas.

      I respectfully refer you to Review: CGI::Prototype with regards to this claim. Now, nearly anything merlyn (or any other expert programmer) will be powerful and well-designed. But, I would hesitate to say it "covers the functionality" of C::A, Zanas, or any other MVC architecture without a side-by-side comparison of said features.

      Not to mention that CGI::Prototype isn't based on Class::Prototyped so much as it uses it. The name, according to merlyn, is more that it is meant to be the prototypical CGI application vs. being a prototyped application. (Which makes me think that he should have called it MVC::Controller::Prototype ... given that he wasn't attempting to replace CGI::Application.)

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.