Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
  • 9 commits
  • 21 files changed
  • 0 commit comments
  • 1 contributor
View
132 lib/Hailo.pm
@@ -2,55 +2,47 @@ package Hailo;
use 5.010;
use autodie qw(open close);
-use Any::Moose;
-use Any::Moose 'X::Types::'.any_moose() => [qw/Int Str Bool HashRef/];
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
use List::Util qw(first);
-use namespace::clean -except => 'meta';
-has brain => (
- isa => Str,
- is => 'rw',
-);
+# Accessors
+for my $method (qw[ brain order _custom_order ]) {
+ no strict 'refs';
+ *{$method} = sub {
+ my ($self, @args) = @_;
+ return $self->{$method} unless @args;
+ return $self->{$method} = $args[0] if @args == 1;
+ die "not implemented";
+ };
+}
-has order => (
- isa => Int,
- is => 'rw',
- default => 2,
- trigger => sub {
- my ($self, $order) = @_;
- $self->_custom_order(1);
- },
-);
+sub new {
+ my ($class, %args) = @_;
-has _custom_order => (
- isa => Bool,
- is => 'rw',
- default => 0,
- init_arg => undef,
- documentation => "Here so we can differentiate between the default value of order being explictly set and being set by default",
-);
+ my $self = {
+ # Defaults
+ order => 2,
+ save_on_exit => 1,
-has save_on_exit => (
- isa => Bool,
- is => 'rw',
- default => 1,
-);
+ engine_class => 'Default',
+ storage_class => 'SQLite',
+ tokenizer_class => 'Words',
+ ui_class => 'ReadLine',
+
+ engine_args => {},
+ storage_args => {},
+ tokenizer_args => {},
+ ui_args => {},
+
+
+ # User options
+ %args,
+ };
+
+ $self->{_custom_order} = 1 if exists $args{order};
+
+ bless $self, $class;
+}
-has brain_resource => (
- documentation => "Alias for `brain' for backwards compatability",
- isa => Str,
- is => 'rw',
- trigger => sub {
- my ($self, $brain) = @_;
- $self->brain($brain);
- },
-);
-
my %has = (
engine => {
name => 'Engine',
@@ -76,49 +68,27 @@ for my $k (keys %has) {
my $method_class = "${k}_class";
my $method_args = "${k}_args";
- # working classes
- has "${k}_class" => (
- isa => Str,
- is => "rw",
- default => $default,
- );
-
- # Object arguments
- has "${k}_args" => (
- documentation => "Arguments for the $name class",
- isa => HashRef,
- coerce => 1,
- is => "ro",
- default => sub { +{} },
- );
-
- # Working objects
- has "_${k}" => (
- does => "Hailo::Role::$name",
- lazy_build => 1,
- is => 'ro',
- init_arg => undef,
- );
-
# Generate the object itself
no strict 'refs';
- *{"_build__${k}"} = sub {
+ *{"_${k}"} = sub {
my ($self) = @_;
- my $obj = $self->_new_class(
+
+ return $self->{"_${k}"} if $self->{"_${k}"};
+ return $self->{"_${k}"} = $self->_new_class(
$name,
- $self->$method_class,
+ $self->{$method_class},
{
- arguments => $self->$method_args,
+ arguments => $self->{$method_args},
($k ~~ [ qw< engine storage > ]
- ? (order => $self->order)
+ ? (order => $self->{order})
: ()),
($k ~~ [ qw< engine > ]
? (storage => $self->_storage)
: ()),
- (($k ~~ [ qw< storage > ] and defined $self->brain)
+ (($k ~~ [ qw< storage > ] and defined $self->{brain})
? (
hailo => $self,
- brain => $self->brain
+ brain => $self->{brain}
)
: ()),
},
@@ -163,15 +133,11 @@ sub _new_class {
}
}
-
-
- if (Any::Moose::moose_is_preferred()) {
- require Class::MOP;
- eval { Class::MOP::load_class($pkg) };
- } else {
+ {
+ local $@;
eval qq[require $pkg];
+ die $@ if $@;
}
- die $@ if $@;
return $pkg->new(%$args);
}
@@ -304,7 +270,7 @@ sub stats {
return $self->_storage->totals();
}
-sub DEMOLISH {
+sub DESTROY {
my ($self) = @_;
$self->save_on_exit if $self->{_storage} and $self->save;
return;
@@ -315,7 +281,7 @@ sub _is_interactive {
return IO::Interactive::is_interactive();
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
48 lib/Hailo/Engine/Default.pm
@@ -1,45 +1,47 @@
package Hailo::Engine::Default;
use 5.010;
-use Any::Moose;
-use Any::Moose 'X::Types::'.any_moose() => [qw< Int >];
+use strict;
+use parent 'Hailo::Role::Engine';
use List::Util qw<min first shuffle>;
use List::MoreUtils qw<uniq>;
-with qw[ Hailo::Role::Arguments Hailo::Role::Engine ];
+# Accessors
+for my $method (qw[ storage order ]) {
+ no strict 'refs';
+ *{$method} = sub {
+ my ($self, @args) = @_;
+ return $self->{$method} unless @args;
+ return $self->{$method} = $args[0] if @args == 1;
+ die "not implemented";
+ };
+}
-has repeat_limit => (
- isa => Int,
- is => 'rw',
- lazy => 1,
- default => sub {
- my ($self) = @_;
- my $order = $self->order;
- return min(($order * 10), 50);
- }
-);
+sub new {
+ my ($class, %args) = @_;
-sub BUILD {
- my ($self) = @_;
+ my $self = {
+ repeat_limit => min(($args{order} * 10), 50),
+ %args,
+ };
# This performance hack is here because in our tight loops calling
# $self->storage->sth->{...} is actually a significant part of the
# overall program execution time since we're doing two method
# calls and hash dereferences for each call to the database.
-
- my $sth = $self->storage->sth;
+ my $sth = $self->{storage}->sth;
while (my ($k, $v) = each %$sth) {
$self->{"_sth_$k"} = $v;
}
- return;
+ bless $self, $class;
}
## no critic (Subroutines::ProhibitExcessComplexity)
sub reply {
my $self = shift;
my $tokens = shift // [];
- my $order = $self->order;
+ my $order = $self->{order};
# we will favor these tokens when making the reply
my @key_tokens = @$tokens;
@@ -73,7 +75,7 @@ sub reply {
# remove key tokens we're already using
@key_ids = grep { my $used = $_; !first { $_ == $used } @token_ids } @key_ids;
- my $repeat_limit = $self->repeat_limit;
+ my $repeat_limit = $self->{repeat_limit};
my $expr_id = $orig_expr_id;
# construct the end of the reply
@@ -123,7 +125,7 @@ sub reply {
sub learn {
my ($self, $tokens) = @_;
- my $order = $self->order;
+ my $order = $self->{order};
# only learn from inputs which are long enough
return if @$tokens < $order;
@@ -266,7 +268,7 @@ sub _random_expr {
}
else {
# try the positions in a random order
- for my $pos (shuffle 0 .. $self->order-1) {
+ for my $pos (shuffle 0 .. $self->{order}-1) {
my $column = "token${pos}_id";
# get a random expression which includes the token at this position
@@ -304,7 +306,7 @@ sub _pos_token {
return $novel_tokens[rand @novel_tokens];
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
11 lib/Hailo/Role/Any.pm
@@ -0,0 +1,11 @@
+package Hailo::Role::Any;
+
+use 5.010;
+use strict;
+
+sub new {
+ my ($class, %args) = @_;
+ bless \%args, $class;
+}
+
+1;
View
12 lib/Hailo/Role/Arguments.pm
@@ -1,16 +1,8 @@
package Hailo::Role::Arguments;
use 5.010;
-use Any::Moose '::Role';
-use Any::Moose 'X::Types::'.any_moose() => [qw/HashRef Str/];
-use namespace::clean -except => 'meta';
-
-has arguments => (
- isa => HashRef[Str],
- is => 'ro',
- documentation => "Arguments passed from Hailo",
- auto_deref => 1,
-);
+
+sub arguments { shift->{arguments} }
1;
View
33 lib/Hailo/Role/Engine.pm
@@ -1,25 +1,20 @@
package Hailo::Role::Engine;
use 5.10.0;
-use Any::Moose '::Role';
-use Any::Moose 'X::Types::'.any_moose() => [qw< Int >];
-use namespace::clean -except => 'meta';
-
-has storage => (
- required => 1,
- is => 'ro',
- documentation => "Our copy of the current Storage object",
-);
-
-has order => (
- required => 1,
- isa => Int,
- is => 'rw',
- documentation => "Our copy of the current markov order",
-);
-
-requires 'learn';
-requires 'reply';
+
+# Accessors
+for my $method (qw[ storage order ]) {
+ no strict 'refs';
+ *{$method} = sub {
+ my ($self, @args) = @_;
+ return $self->{$method} unless @args;
+ return $self->{$method} = $args[0] if @args == 1;
+ die "not implemented";
+ };
+}
+
+sub learn { die }
+sub reply { die }
1;
View
45 lib/Hailo/Role/Storage.pm
@@ -1,31 +1,26 @@
package Hailo::Role::Storage;
use 5.010;
-use Any::Moose '::Role';
-use Any::Moose 'X::Types::'.any_moose() => [qw/Str Int/];
-use namespace::clean -except => 'meta';
-
-has brain => (
- isa => Str,
- is => 'rw',
-);
-
-has order => (
- isa => Int,
- is => 'rw',
-);
-
-has hailo => (
- isa => 'Hailo',
- is => 'ro',
-);
-
-requires 'ready';
-requires 'save';
-requires 'start_learning';
-requires 'stop_learning';
-requires 'start_training';
-requires 'stop_training';
+use parent qw[ Hailo::Role::Any Hailo::Role::Arguments ];
+use strict;
+
+# Accessors
+for my $method (qw[ brain order hailo ]) {
+ no strict 'refs';
+ *{$method} = sub {
+ my ($self, @args) = @_;
+ return $self->{$method} unless @args;
+ return $self->{$method} = $args[0] if @args == 1;
+ die "not implemented";
+ };
+}
+
+sub ready { die }
+sub save { die }
+sub start_learning { die }
+sub stop_learning { die }
+sub start_training { die }
+sub stop_training { die }
sub save {
# does nothing by default
View
21 lib/Hailo/Role/Tokenizer.pm
@@ -1,23 +1,20 @@
package Hailo::Role::Tokenizer;
use 5.010;
-use Any::Moose '::Role';
-use Any::Moose 'X::Types::'.any_moose() => [qw/HashRef Int/];
-use namespace::clean -except => 'meta';
-
-has spacing => (
- isa => HashRef[Int],
- is => 'rw',
- default => sub { {
+use parent 'Hailo::Role::Any';
+use strict;
+
+sub spacing {
+ return +{
normal => 0,
prefix => 1,
postfix => 2,
infix => 3,
- } },
-);
+ };
+}
-requires 'make_tokens';
-requires 'make_output';
+sub make_tokens { die }
+sub make_output { die }
1;
View
6 lib/Hailo/Role/UI.pm
@@ -1,10 +1,10 @@
package Hailo::Role::UI;
use 5.010;
-use Any::Moose '::Role';
-use namespace::clean -except => 'meta';
+use strict;
+use parent 'Hailo::Role::Any';
-requires 'run';
+sub run { die }
1;
View
87 lib/Hailo/Storage.pm
@@ -1,60 +1,38 @@
package Hailo::Storage;
use 5.010;
-use Any::Moose;
-use Any::Moose 'X::Types::'.any_moose() => [qw<ArrayRef HashRef Int Str Bool>];
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
+use strict;
+use parent 'Hailo::Role::Storage';
use DBI;
use Hailo::Storage::Schema;
-has dbd => (
- isa => Str,
- is => 'ro',
- lazy_build => 1,
- documentation => "The DBD::* driver we're using",
-);
-
-has dbd_options => (
- isa => HashRef,
- is => 'ro',
- lazy_build => 1,
- documentation => 'Options passed as the last argument to DBI->connect()',
-);
-
-sub _build_dbd_options {
+# Accessors
+for my $method (qw[ _engaged _boundary_token_id ]) {
+ no strict 'refs';
+ *{$method} = sub {
+ my ($self, @args) = @_;
+ return $self->{$method} unless @args;
+ return $self->{$method} = $args[0] if @args == 1;
+ die "not implemented";
+ };
+}
+
+sub dbd_options {
my ($self) = @_;
return {
RaiseError => 1
};
}
-has dbh => (
- isa => 'DBI::db',
- is => 'ro',
- lazy_build => 1,
- documentation => 'Our DBD object',
-);
-
-sub _build_dbh {
+sub dbh {
my ($self) = @_;
- my $dbd_options = $self->dbi_options;
-
- return DBI->connect($self->dbi_options);
+ return $self->{dbh} // ($self->{dbh} = do {
+ my $dbd_options = $self->dbi_options;
+ DBI->connect(@{ $self->dbi_options });
+ });
};
-has dbi_options => (
- isa => ArrayRef,
- is => 'ro',
- auto_deref => 1,
- lazy_build => 1,
- documentation => 'Options passed to DBI->connect()',
-);
-
-sub _build_dbi_options {
+sub dbi_options {
my ($self) = @_;
my $dbd = $self->dbd;
my $dbd_options = $self->dbd_options;
@@ -70,30 +48,11 @@ sub _build_dbi_options {
return \@options;
}
-has _engaged => (
- isa => Bool,
- is => 'rw',
- default => 0,
- documentation => 'Have we done setup work to get this database going?',
-);
-
-has sth => (
- isa => HashRef,
- is => 'ro',
- lazy_build => 1,
- documentation => 'A HashRef of prepared DBI statement handles',
-);
-
-sub _build_sth {
+sub sth {
my ($self) = @_;
- return Hailo::Storage::Schema->sth($self->dbd, $self->dbh, $self->order);
+ return $self->{sth} // ($self->{sth} = Hailo::Storage::Schema->sth($self->dbd, $self->dbh, $self->order));
}
-has _boundary_token_id => (
- isa => Int,
- is => 'rw',
-);
-
# bootstrap the database
sub _engage {
my ($self) = @_;
@@ -192,7 +151,7 @@ sub totals {
return $token, $expr, $prev, $next;
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
25 lib/Hailo/Storage/MySQL.pm
@@ -1,28 +1,21 @@
package Hailo::Storage::MySQL;
use 5.010;
-use Any::Moose;
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
+use strict;
+use parent 'Hailo::Storage';
use List::MoreUtils qw< all >;
-use namespace::clean -except => 'meta';
-
-extends 'Hailo::Storage';
-with qw(Hailo::Role::Arguments Hailo::Role::Storage);
-sub _build_dbd { return 'mysql' };
+sub dbd { 'mysql' };
-override _build_dbd_options => sub {
+sub dbd_options {
+ my ($self) = @_;
return {
- %{ super() },
+ %{ $self->SUPER::dbd_options },
mysql_enable_utf8 => 1,
};
-};
+}
-sub _build_dbi_options {
+sub dbi_options {
my ($self) = @_;
my $dbd = $self->dbd;
my $dbd_options = $self->dbd_options;
@@ -55,7 +48,7 @@ sub ready {
return all { exists $self->arguments->{$_} } qw(database username password);
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
25 lib/Hailo/Storage/PostgreSQL.pm
@@ -1,27 +1,20 @@
package Hailo::Storage::PostgreSQL;
use 5.010;
-use Any::Moose;
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
-use namespace::clean -except => 'meta';
-
-extends 'Hailo::Storage';
-with qw(Hailo::Role::Arguments Hailo::Role::Storage);
+use strict;
+use parent 'Hailo::Storage';
-sub _build_dbd { return 'Pg' };
+sub dbd { 'Pg' };
-override _build_dbd_options => sub {
+sub dbd_options {
+ my ($self) = @_;
return {
- %{ super() },
+ %{ $self->SUPER::dbd_options },
pg_enable_utf8 => 1,
};
-};
+}
-sub _build_dbi_options {
+sub dbi_options {
my ($self) = @_;
my $dbd = $self->dbd;
my $dbd_options = $self->dbd_options;
@@ -55,7 +48,7 @@ sub ready {
return exists $self->arguments->{dbname};
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
50 lib/Hailo/Storage/SQLite.pm
@@ -1,28 +1,19 @@
package Hailo::Storage::SQLite;
use 5.010;
-use Any::Moose;
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
-use namespace::clean -except => 'meta';
-
-extends 'Hailo::Storage';
-with qw(Hailo::Role::Arguments Hailo::Role::Storage);
+use parent 'Hailo::Storage';
-sub _build_dbd { return 'SQLite' };
+sub dbd { 'SQLite' };
-override _build_dbd_options => sub {
+sub dbd_options {
+ my ($self) = @_;
return {
- %{ super() },
+ %{ $self->SUPER::dbd_options },
sqlite_unicode => 1,
};
-};
+}
-around _build_dbi_options => sub {
- my $orig = shift;
+sub dbi_options {
my $self = shift;
my $return;
@@ -33,11 +24,11 @@ around _build_dbi_options => sub {
$self->brain($file);
}
else {
- $return = $self->$orig(@_);
+ $return = $self->SUPER::dbi_options(@_);
}
return $return;
-};
+}
# Are we running in a mixed mode where we run in memory but
# restore/backup to disk?
@@ -49,8 +40,8 @@ sub _backup_memory_to_disk {
and $self->arguments->{in_memory});
}
-before _engage => sub {
- my ($self) = @_;
+sub _engage {
+ my $self = shift;
# Set any user-defined pragmas
$self->_set_pragmas;
@@ -59,18 +50,23 @@ before _engage => sub {
$self->dbh->sqlite_backup_from_file($self->brain);
}
- return;
+ return $self->SUPER::_engage(@_);
};
-before start_training => sub {
- my $dbh = shift->dbh;
+sub start_training {
+ my $self = shift;
+ my $dbh = $self->dbh;
$dbh->do('PRAGMA synchronous=OFF;');
$dbh->do('PRAGMA journal_mode=OFF;');
- return;
+ return $self->SUPER::start_training(@_);
};
-after stop_training => sub {
- my $dbh = shift->dbh;
+sub stop_training {
+ my $self = shift;
+ my $dbh = $self->dbh;
+
+ $self->SUPER::stop_training(@_);
+
$dbh->do('PRAGMA journal_mode=DELETE;');
$dbh->do('PRAGMA synchronous=ON;');
return;
@@ -127,7 +123,7 @@ sub save {
return;
};
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
14 lib/Hailo/Tokenizer/Chars.pm
@@ -1,16 +1,8 @@
package Hailo::Tokenizer::Chars;
use 5.010;
-use Any::Moose;
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
-use namespace::clean -except => 'meta';
-
-with qw(Hailo::Role::Arguments
- Hailo::Role::Tokenizer);
+use parent 'Hailo::Role::Tokenizer';
+use strict;
# output -> tokens
sub make_tokens {
@@ -33,7 +25,7 @@ sub trim {
return $txt;
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
13 lib/Hailo/Tokenizer/Words.pm
@@ -1,17 +1,8 @@
package Hailo::Tokenizer::Words;
use 5.010;
+use parent 'Hailo::Role::Tokenizer';
use utf8;
-use Any::Moose;
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
-use namespace::clean -except => 'meta';
-
-with qw(Hailo::Role::Arguments
- Hailo::Role::Tokenizer);
# tokenization
my $DECIMAL = qr/[.,]/;
@@ -131,7 +122,7 @@ sub make_output {
return $reply;
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
20 lib/Hailo/UI/ReadLine.pm
@@ -1,23 +1,15 @@
package Hailo::UI::ReadLine;
use 5.010;
-use Any::Moose;
-BEGIN {
- return unless Any::Moose::moose_is_preferred();
- require MooseX::StrictConstructor;
- MooseX::StrictConstructor->import;
-}
-use Encode 'decode';
+use strict;
+use parent 'Hailo::Role::UI';
use Hailo;
use Term::ReadLine;
-use namespace::clean -except => 'meta';
-
-with qw(Hailo::Role::Arguments
- Hailo::Role::UI);
-sub BUILD {
+sub new {
$ENV{PERL_RL} = 'Perl o=0' unless $ENV{PERL_RL};
- return;
+
+ shift->SUPER::new(@_);
}
sub run {
@@ -35,7 +27,7 @@ sub run {
return;
}
-__PACKAGE__->meta->make_immutable;
+1;
=encoding utf8
View
4 t/00_diag.t
@@ -1,11 +1,9 @@
use 5.010;
use strict;
-use Any::Moose;
use Test::More tests => 1;
use Hailo;
my $version = $Hailo::VERSION // 'dev-git';
-my $m = any_moose();
-diag("Testing Hailo $version with $^X $] using $m for Moose");
+diag("Testing Hailo $version with Perl $^X");
pass("Token test");
View
4 t/command/run.t
@@ -8,9 +8,7 @@ use Hailo::Command;
use Test::More tests => 17;
SKIP: {
- if (Any::Moose::mouse_is_preferred()) {
- skip "Mouse doesn't have X::StrictConstructor", 1;
- }
+ skip "We don't do this anymore because we don't have Moose", 1;
dies_ok { Hailo::Command->new( qw( a b c d ) ) } "Hailo dies on unknown arguments";
}
View
6 t/hailo/methods.t
@@ -1,7 +1,5 @@
use 5.010;
use autodie;
-use strict;
-use warnings;
use List::MoreUtils qw(uniq);
use Test::More tests => 22;
use Test::Exception;
@@ -14,9 +12,7 @@ $SIG{__WARN__} = sub {
# new
SKIP: {
- if (Any::Moose::mouse_is_preferred()) {
- skip "Mouse doesn't have X::StrictConstructor", 1;
- }
+ skip "We don't do this anymore because we don't have Moose", 1;
dies_ok { Hailo->new( qw( a b c d ) ) } "Hailo dies on unknown arguments";
}
View
8 t/hailo/methods/order.t
@@ -8,10 +8,12 @@ use Test::Exception;
# errors during DEMOLISH
use Hailo;
-use Hailo::Command;
-dies_ok { Hailo->new( order => undef ) } "undef order";
-dies_ok { Hailo->new( order => "foo" ) } "Str order";
+SKIP: {
+ skip "We don't do this anymore because we don't have Moose", 2;
+ dies_ok { Hailo->new( order => undef ) } "undef order";
+ dies_ok { Hailo->new( order => "foo" ) } "Str order";
+}
for (my $i = 1; $i <= 10e2; $i += $i * 2) {
cmp_ok( Hailo->new( order => $i )->order, '==', $i, "The order is what we put in ($i)" );
View
97 t/lib/Hailo/Test.pm
@@ -1,7 +1,7 @@
package Hailo::Test;
use 5.010;
+use strict;
use autodie;
-use Any::Moose;
use Hailo;
use Test::More;
use File::Spec::Functions qw(catfile);
@@ -10,7 +10,17 @@ use List::Util qw(shuffle min);
use File::Temp qw(tempfile tempdir);
use File::CountLines qw(count_lines);
use Hailo::Tokenizer::Words;
-use namespace::clean -except => 'meta';
+
+# Accessors
+for my $method (qw[ storage in_memory brain exhaustive brief ]) {
+ no strict 'refs';
+ *{$method} = sub {
+ my ($self, @args) = @_;
+ return $self->{$method} unless @args;
+ return $self->{$method} = $args[0] if @args == 1;
+ die "not implemented";
+ };
+}
sub all_storages {
return qw(DBD::SQLite DBD::Pg DBD::mysql);
@@ -28,32 +38,24 @@ sub exhaustive_tests {
return (all_tests(), qw(test_timtoady));
}
-has brief => (
- is => 'ro',
- isa => 'Bool',
- default => 0,
-);
+sub new {
+ my ($class, %args) = @_;
-has in_memory => (
- is => 'ro',
- isa => 'Bool',
- default => 1,
-);
-
-has exhaustive => (
- is => 'ro',
- isa => 'Bool',
- default => 0,
-);
+ my $self = {
+ brief => 0,
+ in_memory => 1,
+ exhaustive => 0,
+ %args,
+ };
-has tmpdir => (
- is => 'ro',
- isa => 'Str',
- lazy_build => 1,
-);
+ bless $self, $class;
+}
-sub _build_tmpdir {
+sub tmpdir {
my ($self) = @_;
+
+ return $self->{tmpdir} if $self->{tmpdir};
+
my $storage = $self->storage;
$storage =~ s/[^A-Za-z0-9]/-/g;
@@ -61,45 +63,34 @@ sub _build_tmpdir {
# Dir to store our brains
my $dir = tempdir( "hailo-test-$storage-XXXXX", CLEANUP => 1, TMPDIR => 1 );
- return $dir;
+ return $self->{tmpdir} = $dir;
}
-has brain => (
- is => 'ro',
- isa => 'Str',
-);
-
-has tmpfile => (
- is => 'ro',
- lazy_build => 1,
-);
-
-sub _build_tmpfile {
+sub tmpfile {
my ($self) = @_;
+ return $self->{tmpfile} if $self->{tmpfile};
+
# Dir to store our brains
my $dir = $self->tmpdir;
my ($fh, $filename) = tempfile( DIR => $dir, SUFFIX => '.trn', EXLOCK => 0 );
$fh->autoflush(1);
- return [$fh, $filename];
+ return $self->{tmpfile} = [$fh, $filename];
}
-has hailo => (
- is => 'ro',
- isa => "Hailo",
- lazy_build => 1,
-);
-
-sub _build_hailo {
+sub hailo {
my ($self) = @_;
+
+ return $self->{hailo} if $self->{hailo};
+
my $storage = $self->storage;
my %opts = $self->_connect_opts;
my $hailo = Hailo->new(%opts);
- return $hailo;
+ return $self->{hailo} = $hailo;
}
sub get_brain {
@@ -132,12 +123,7 @@ sub spawn_storage {
if (exists $classes{$storage}) {
my $pkg = $classes{$storage};
- if (Any::Moose::moose_is_preferred()) {
- require Class::MOP;
- eval { Class::MOP::load_class($pkg) };
- } else {
- eval qq[require $pkg];
- }
+ eval qq[require $pkg];
return if $@;
}
@@ -238,11 +224,6 @@ sub _connect_opts {
return %all_opts;
}
-has storage => (
- is => 'ro',
- isa => 'Str',
-);
-
# learn from various sources
sub train_megahal_trn {
my ($self) = @_;
@@ -562,7 +543,7 @@ sub test_file {
return $path;
}
-sub DEMOLISH {
+sub DESTROY {
my ($self) = @_;
my $hailo = $self->hailo;
my $storage = $self->storage;
@@ -570,4 +551,4 @@ sub DEMOLISH {
$self->unspawn_storage();
}
-__PACKAGE__->meta->make_immutable;
+1;
View
9 t/lib/Hailo/Test/Tokenizer.pm
@@ -1,12 +1,11 @@
package Hailo::Test::Tokenizer;
use 5.010;
-use Any::Moose;
-use namespace::clean -except => 'meta';
-use Hailo::Tokenizer::Words;
+use strict;
+use parent 'Hailo::Role::Tokenizer';
-with 'Hailo::Role::Tokenizer';
+die "XXX: This isn't called anymore!";
sub make_tokens { goto &Hailo::Tokenizer::Words::make_tokens }
sub make_output { goto &Hailo::Tokenizer::Words::make_output }
-__PACKAGE__->meta->make_immutable;
+1;

No commit comments for this range

Something went wrong with that request. Please try again.