restricted code
Matt S Trout [Sun, 8 Apr 2012 19:47:07 +0000 (19:47 +0000)]
lib/Role/Tiny/Restricted.pm [new file with mode: 0644]
lib/Role/Tiny/Restricted/With.pm [new file with mode: 0644]

diff --git a/lib/Role/Tiny/Restricted.pm b/lib/Role/Tiny/Restricted.pm
new file mode 100644 (file)
index 0000000..713e431
--- /dev/null
@@ -0,0 +1,18 @@
+package Role::Tiny::Restricted;
+
+use strict;
+use warnings FATAL => 'all';
+use base qw(Role::Tiny);
+
+sub apply_union_of_roles_to_package {
+  my ($me, $to, @roles) = @_;
+  my %app = %{$Role::Tiny::APPLIED_TO{$to}||{}};
+  delete $app{$to};
+  if (%app) {
+    require Carp;
+    Carp::confess("with() may not be called more than once for $to");
+  }
+  $me->SUPER::apply_union_of_roles_to_package($to, @roles);
+}
+
+1;
diff --git a/lib/Role/Tiny/Restricted/With.pm b/lib/Role/Tiny/Restricted/With.pm
new file mode 100644 (file)
index 0000000..06c1958
--- /dev/null
@@ -0,0 +1,15 @@
+package Role::Tiny::Restricted::With;
+
+use strict;
+use warnings FATAL => 'all';
+use Role::Tiny::Restricted ();
+
+use Exporter 'import';
+our @EXPORT = qw( with );
+
+sub with {
+    my $target = caller;
+    Role::Tiny::Restricted->apply_union_of_roles_to_package($target, @_)
+}
+
+1;