Lyle Brooks <[EMAIL PROTECTED]> said something to this effect on 02/05/2002:
> This thread makes me wonder if there are "Best Practices"
> guidelines for when and perhaps when not to create Plugin.

[-- snip --]

> Anyone want to share when a Plugin is either the "only" way or
> the preferred way to go vs. using LOAD_PERL?

Personally, I prefer to use virtual methods, then sub refs passed
to process, then a Plugin, then a filter, when I add
functionality.

Whenever possible, I make simple operations that operate on a
single variable into virtual methods:

  sub commify {
      my $v = shift;
      1 while $v =~ s/^([-+]?\d+)(\d{3})/$1,$2/;
      $v;
  }

  $Template::Stash::SCALAR_OPS->{'md5'} = \&Digest::MD5::md5_hex;
  $Template::Stash::SCALAR_OPS->{'commify'} = \&commify;

With these, for example, you can do:

  The MD5 checksum of '[% foo %]' is [% foo.md5 %].
  The commified version of '[% num %]' is [% num.commify %].

And, assuming foo is "foo" and "num" is 1234567, you get:

  The MD5 checksum of 'foo' is acbd18db4cc2f85cedef654fccc4a4d8.
  The commified version of '1234567' is 1,234,567.

If you don't want to go this route, then another option looks
like:

  my %params = (
      'commify' => \&commify,
      'md5' => \&Digest::MD5::md5_hex,
  );
  $tt->process($file, \%params);

In this case, use them as such:

  The MD5 checksum of '[% foo %]' is [% md5(foo) %].
  The commified version of '[% num %]' is [% commify(num) %].

And you get results identical to those above.

Other good examples for virtual methods are uc, lc, and all those
sorts of functions.

For things of this nature that operate on blocks, a Filter is the
way to go.

I use Plugins when ever I need to do some translation between the
module I am calling and the data available from the template.
Plugins can also be used to save state when using subroutines
from stateless modules (for example, saving the logs from
Mail::Sendmail).  Finally, LOAD_PERL is only usful for OO
modules.  If you have modules that export subroutines, LOAD_PERL
doesn't help you.

Helpful?

(darren)

-- 
The bad reputation UNIX has gotten is totally undeserved, laid on
by people who don't understand, who have not gotten in there and
tried anything.
    -- Jim Joyce, owner of Jim Joyce's UNIX Bookstore


Reply via email to