From: Matt S Trout Date: Tue, 23 Apr 2019 00:15:03 +0000 (+0000) Subject: add with.pm to extras X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b73fb1b622b1a1d15916d32e2a4bf102987490e1;p=scpubgit%2FQ-Branch.git add with.pm to extras --- diff --git a/lib/with.pm b/lib/with.pm new file mode 100644 index 0000000..28055b4 --- /dev/null +++ b/lib/with.pm @@ -0,0 +1,38 @@ +package with; + +# This must be its own dist later + +use strict; +use warnings; +use if $] < '5.010', 'MRO::Compat'; +use mro; + +my $comp = 'A001'; + +sub components { + my ($inv, @comp) = @_; + my $class = ref($inv) || $inv; + my $new_class = join('::', $class, $comp++); + require Class::C3::Componentised; + my @comp_classes = map +(/^\+(.+)$/ ? "${class}::$1" : $_), @comp; + Class::C3::Componentised->ensure_class_loaded($_) for @comp_classes; + Class::C3::Componentised->inject_base( + $new_class, + @comp_classes, $class, + ); + return $new_class unless ref($inv); + return bless($inv, $new_class); +} + +sub roles { + my ($inv, @roles) = @_; + my $class = ref($inv) || $inv; + require Role::Tiny; + my $new_class = Role::Tiny->create_class_with_roles($class, + map +(/^\+(.+)$/ ? "${class}::$1" : $_), @roles + ); + return $new_class unless ref($inv); + return bless($inv, $new_class); +} + +1;