diff options
Diffstat (limited to 'lib/DAKKAR/p.pm')
-rw-r--r-- | lib/DAKKAR/p.pm | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/lib/DAKKAR/p.pm b/lib/DAKKAR/p.pm new file mode 100644 index 0000000..ced4be8 --- /dev/null +++ b/lib/DAKKAR/p.pm @@ -0,0 +1,102 @@ +package DAKKAR::p; +use 5.012; +use strict; +use warnings; +use utf8 (); +use feature (); +use true (); +use TryCatch (); +use Carp (); +use Sub::Import (); +use namespace::autoclean; +use B::Hooks::EndOfScope; +use Hook::AfterRuntime; +use autobox (); +use Moose::Autobox (); + +sub import { + my ($class,@opts) = @_; + my $caller = caller; + + strict->import(); + warnings->import('FATAL'=>'all'); + feature->import( ':5.12' ); + utf8->import($caller); + true->import(); + TryCatch->import({into=>$caller}); + Sub::Import->import('Carp',{into=>$caller}); + Moose::Autobox->import(); + + for (@opts) { + when ('class') { + require Moose; + require MooseX::Params::Validate; + Moose->import({into=>$caller}); + MooseX::Params::Validate->import({into=>$caller}); + after_runtime { + $caller->meta->make_immutable; + } + }; + when ('role') { + require Moose::Role; + require MooseX::Params::Validate; + Moose::Role->import({into=>$caller}); + MooseX::Params::Validate->import({into=>$caller}); + }; + when ('exporter') { + on_scope_end { + __PACKAGE__->mark_as_method('import',$caller); + } + }; + when ('test') { + require lib; + lib->import('t/lib'); + # yes, this is ugly, but I couldn't find a better way; + eval <<"MAGIC" or die "Couldn't set up testing policy: $@"; +package $caller; +use Test::Most '-Test::Deep'; +use Test::Deep '!blessed'; +use Data::Printer; +1; +MAGIC + } + } + + # this must come after the on_scope_end call above, otherwise the + # clean happens before the mark_as_method, and 'import' is cleaned + # even though we don't want it to be + namespace::autoclean->import( + -cleanee => $caller, + ); +} + +sub mark_as_method { + my ($self,$method_name,$class)=@_; + + $class //= caller; + + my $meta=Class::MOP::Class->initialize($class); + return if $meta->has_method($method_name); + my $code = $meta->get_package_symbol({ + name => $method_name, + sigil => '&', + type => 'CODE', + }); + + do { warn "$method_name not found as a CODE symbol!"; return } + unless defined $code; + + $meta->add_method( + $method_name => ( + $meta->wrap_method_body( + associated_metaclass => $meta, + name => $method_name, + body => $code, + ), + ) + ); + + return; +} + +1; |