Revision history for Perl extension Moose
0.15
+
+ * Moose::Util::TypeConstraints
+ - added &unimport so that you can clean out
+ your class namespace of these exported
+ keywords
+
* Moose::Meta::Class
- fixed minor issue which occasionally
comes up during global destruction
use Carp 'confess';
use Scalar::Util 'blessed';
+use B 'svref_2object';
+use Sub::Exporter;
-our $VERSION = '0.08';
+our $VERSION = '0.09';
use Moose::Meta::TypeConstraint;
use Moose::Meta::TypeCoercion;
-use Sub::Exporter -setup => {
- exports => [qw/
- type subtype as where message
- coerce from via
- enum
- find_type_constraint
- /],
- groups => {
- default => [':all']
+my @exports = qw/
+ type subtype as where message
+ coerce from via
+ enum
+ find_type_constraint
+/;
+
+Sub::Exporter::setup_exporter({
+ exports => \@exports,
+ groups => { default => [':all'] }
+});
+
+sub unimport {
+ no strict 'refs';
+ my $class = caller();
+ # loop through the exports ...
+ foreach my $name (@exports) {
+ # if we find one ...
+ if (defined &{$class . '::' . $name}) {
+ my $keyword = \&{$class . '::' . $name};
+
+ # make sure it is from Moose
+ my $pkg_name = eval { svref_2object($keyword)->GV->STASH->NAME };
+ next if $@;
+ next if $pkg_name ne 'Moose::Util::TypeConstraints';
+
+ # and if it is from Moose then undef the slot
+ delete ${$class . '::'}{$name};
+ }
}
-};
+}
{
my %TYPES;
=back
+=head2 Namespace Management
+
+=over 4
+
+=item B<unimport>
+
+This will remove all the type constraint keywords from the
+calling class namespace.
+
+=back
+
=head1 BUGS
All complex software has bugs lurking in it, and this module is no
use strict;
use warnings;
-use Test::More tests => 23;
+use Test::More tests => 45;
BEGIN {
use_ok('Moose');
ok(!Foo->can($_), '... Foo can no longer do ' . $_) for @moose_exports;
can_ok('Foo', $_) for @moose_not_unimported;
+# and check the type constraints as well
+
+my @moose_type_constraint_exports = qw(
+ type subtype as where message
+ coerce from via
+ enum
+ find_type_constraint
+);
+
+{
+ package Bar;
+}
+
+eval q{
+ package Bar;
+ use Moose::Util::TypeConstraints;
+};
+ok(!$@, '... Moose::Util::TypeConstraints succesfully exported into Bar');
+
+can_ok('Bar', $_) for @moose_type_constraint_exports;
+
+eval q{
+ package Bar;
+ no Moose::Util::TypeConstraints;
+};
+ok(!$@, '... Moose::Util::TypeConstraints succesfully un-exported from Bar');
+
+ok(!Bar->can($_), '... Bar can no longer do ' . $_) for @moose_type_constraint_exports;
+