On Fri, 6 Jun 2003, darren chamberlain wrote:

> It occurs to me that if the second line of Template::Stash::new were
> changed to:
>
>   my $params = UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
>
> It would work the way you expected...

That won't allow you to call the methods in the object, will it?

I'd be tempted to do something like this:

  use Scalar::Util qw(blessed);
  my $params;
  if (blessed($_[0]))
  {
    $params = {}; See comment [1].
    my $that = $_[0];

    foreach my $method (methods_of(ref($_[0]))
    {
      $params->{ $method } = sub { $that->$method(@_) };
    }
  }
  else
  {
    $params = ref $_[0] eq 'HASH' ? shift(@_) : { @_ };
  }

Problems with this:

 1) I don't know how to get all the methods for a class...I'm not sure
    it's even possible is it?  Maybe this could be implemented with Andy's
    solution for a 'not-found' stash thingy instead?  I.e. have a 'if you
    passed in a blessed object and there was nothing in the stash, see if
    you can call a method of that name and then call it if you can' type
    solution.

 2) Scalar::Util is currently not a dependancy of TT.  I suggest we
    simply reimplement (er, copy) the code for 'blessed' from it.

[1] This could be a shallow copy.  so 'my $hash = { %{ $_[0] } }', though
that breaks encapsulation all over the shot.

-- 
#!/usr/bin/perl -T
use strict;
use warnings;
print q{Mark Fowler, [EMAIL PROTECTED], http://twoshortplanks.com/};

_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to