* Mark Fowler <[EMAIL PROTECTED]> [2003-02-03 07:30]:
> I'd be loathed to add further virtual methods as standard though.
> Maybe we could do with a plugin somewhat like Scalar::Util that adds
> many virtual methods that are virtually 'core' but arn't mainly to
> stop feature creep.
> 
> Comments?

I'm all for adding new virtual methods -- each one is a single static
subroutine maintained in the Template::Stash package, there's very
little overheard (especially when you consider that TT2 is evaling huge
strings all the time anyway).

However, for the sanity of those who have to add to/maintain the virtual
methods, I think I would break them out into a separate .pm file, which
Template::Stash uses and which declares its package as Template::Stash :

  # Template/VMeth.pm
  package Template::Stash;

  use vars qw( $HASH_OPS $SCALAR_OPS $LIST_OPS );

  $SCALAR_OPS = {
    # and so on

  # Template/Stash.pm
  use Template::VMeth;

Template::VMeth would only be compiled once, and the virtual methods
would live in the Template::Stash namespace still (so people manually
adding virtual methods using, e.g.:

  $Template::Stash::SCALAR_OPS->{'int'} = sub { int $_[0] };

wouldn't have to change their code).

While we're at it, I'd also like to see an official method to add
virtual methods to a stash; bonus points if those vmeths could be
localized to a particular instance.  I'd considered working this in, but
it required more changes to Template::Stash than I had time for:

  # For the class:
  Template::Stash->add_scalar_op('int', sub { int $_[0] });
  Template::Stash->add_hash_op('delete', sub { delete $_[0]->{$_[1]} });

  # For an instance:
  my %VMETHS = (
        SCALAR => {
            'int' => sub { int $_[0] },
        },
        HASH => {
            'delete' => sub { delete $_[0]->{$_[1]} },
        },
  );
  my $stash = Template::Config->stash(VMETHS => \%VMETHS);

(darren)

-- 
I invented the term "Object-Oriented", and I can tell you, I didn't
have C++ in mind.
    -- Alan Kay

_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.ourshack.com/mailman/listinfo/templates

Reply via email to