Axeman
Axeman

Reputation: 29854

Moose or Meta?

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

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] };

Upvotes: 6

Views: 513

Answers (2)

Penfold
Penfold

Reputation: 2568

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

Upvotes: 3

slf
slf

Reputation: 22767

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

Upvotes: 2

Related Questions