Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moose or Meta?

Tags:

perl

moose

I've been trying to do this a number of ways, but none of them seem graceful enough. (I'm also wondering if CPAN or Moose already has this. The dozens of searches I've done over time have shown nothing that quite matches.)

I want to create a type of class that

  • is a Base + Facade + Factory for other classes which load themselves as destination types.
  • The "factory" is just Base->new( %params ) and this creates types based on policies registered by the individual subclass.
  • Each subclass knows basic things about the domain of the Base class, but I'm trying to keep it minimal. See the example below: UnresolvedPath just knows that we should check for existence first.

The obvious example for this is file system directories and files:

package Path;
use Moose;

...

sub BUILD { 
    my ( $self, $params ) = @_;
    my $path = $params->{path};

    my $class_name;
    foreach my $test_sub ( @tests ) { 
        $class_name = $test_sub->( $path );
        last if $class_name;
    }
    croak "No valid class for $path!" unless defined $class_name;
    $class_name->BUILD( $self, $params );
}

package Folder; 
use Moose;

extends 'Path';

use Path register => selector => sub { -d $_[0] };

sub BUILD { ... }

package UnresolvedPath; 

extends 'Path';

use Path register position => 1, selector => sub { !-e $_[0] };
  • Question: Does Moose provide a graceful solution to this? Or would I have to go into Class::MOP for the bulk?
like image 993
Axeman Avatar asked Mar 16 '09 16:03

Axeman


2 Answers

Have a peek at http://code2.0beta.co.uk/moose/svn/MooseX-AbstractFactory/ and feel free to steal. (Mine.)

like image 63
Penfold Avatar answered Nov 08 '22 08:11

Penfold


If you truely want to do the Builder Pattern or the Abstract Factory Pattern then you can do that, and there is nothing stopping you. But perhaps what you really need is some Inversion of Control / Dependency Injection? For that, you can checkout Bread Board

like image 29
slf Avatar answered Nov 08 '22 10:11

slf