On Sun, Apr 19, 2009 at 07:58:18AM +0800, Fayland Lam wrote:
> package Test;
> 
> use Moose;
> 
> sub new {
>     my $class = shift;
>     my $params = $class->BUILDARGS(@_);
> 
>     my $obj;
>     if ( $params->{ver} == 1 ) {
>         require T::V1;
>         $obj = T::V1->new($params);
>     } else {
>         require T::V2;
>         $obj = T::V2->new($params);
>     }
> 
>     return $class->meta->new_object(
>         __INSTANCE__ => $obj,
>         @_,
>     );
> }

I can't imagine a situation where this is correct.  At best, it's just
confusing -- you're calling Test->meta->new_object, but returning a T::V1 or a
T::V2, and I'm pretty sure it's an undocumented feature that new_object doesn't
e.g. rebless into Test there.

You're barely using Moose here, and the parts that you are using, you're using
in a confusing and nonstandard way.  Much simpler would be:

  package Test;

  sub new {
    my $class = shift;
    my $params = (@_ == 1 and ref $_[0] eq 'HASH') ? $_[0] : {...@_};
    my $version = $params->{ver} ||= 2;
    my $t_class = "T::V$version";
    Class::MOP::load_class($t_class);
    return $t_class->new($params);
  }

This isn't what Moose means by "delegation", btw; see "handles" for that.

hdp.

Reply via email to