DSL::Tiny::InstanceEval - Add DSL features to your class.
version 0.001
use Test::More; use Test::Deep; # put together class with a simple dsl { package MyClassWithDSL; use Moo; # or Moose with qw(DSL::Tiny::Role DSL::Tiny::InstanceEval); sub _build_dsl_keywords { [ qw(add_values) ] }; has values => (is => 'ro', default => sub { [] }, ); sub add_values { my $self = shift; push @{$self->values}, @_; } } # make a new instance my $dsl = MyClassWithDSL->new(); my $code = <<EOC; add_values(qw(2 1)); add_values(qw(3)); EOC my $return_value = $dsl->instance_eval($code); cmp_deeply($dsl->values, bag(qw(1 2 3)), "Values were added"); done_testing;
This is an initial release. It's all subject to rethinking. Comments welcome.
This package provides a simple interface, "instance_eval", for evaluating snippets of a DSL (implemented with DSL::Tiny::Role) with respect to a particular instance of a class that consumes the role.
Private attribute, used to set up a package to stash private stuff.
PRIVATE
There is no 'u' in _instance_evalator. That means there should be no you in there either....
Returns a coderef that is used by the instance_eval() method.
Something kind-a-similar to Ruby's instance_eval. Takes a string and evaluates it using eval(), The evaluation happens in a package that has been populated with a set of functions that map to methods in this class with the instance curried out.
See the synopsis for an example.
Requires _build_dsl_keywords, as a proxy for being used in a class that consumes DSL::Tiny::Role.
George Hartzell <hartzell@alerce.com>
This software is copyright (c) 2013 by George Hartzell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install DSL::Tiny::Role, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DSL::Tiny::Role
CPAN shell
perl -MCPAN -e shell install DSL::Tiny::Role
For more information on module installation, please visit the detailed CPAN module installation guide.