From: gfx Date: Tue, 6 Oct 2009 06:34:35 +0000 (+0900) Subject: Use Mouse::Exporter X-Git-Tag: 0.37_03~30 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fea36fd29b45961b8687f1aee7fd2701c984fae4;p=gitmo%2FMouse.git Use Mouse::Exporter --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index 047ab4e..0e12516 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -6,10 +6,8 @@ use warnings; our $VERSION = '0.37_02'; -use Exporter; - -use Carp 'confess'; -use Scalar::Util 'blessed'; +use Carp qw(confess); +use Scalar::Util (); use Mouse::Util qw(load_class is_class_loaded get_code_package not_supported); @@ -19,23 +17,21 @@ use Mouse::Meta::Role; use Mouse::Meta::Attribute; use Mouse::Object; use Mouse::Util::TypeConstraints (); - -our @ISA = qw(Exporter); - -our @EXPORT = qw( - extends with - has - before after around - override super - augment inner - - blessed confess +use Mouse::Exporter; + +Mouse::Exporter->setup_import_methods( + as_is => [qw( + extends with + has + before after around + override super + augment inner + ), + \&Scalar::Util::blessed, + \&Carp::confess, + ], ); -our %is_removable = map{ $_ => undef } @EXPORT; -delete $is_removable{blessed}; -delete $is_removable{confess}; - sub extends { Mouse::Meta::Class->initialize(scalar caller)->superclasses(@_) } sub has { @@ -126,9 +122,6 @@ sub init_meta { my $base_class = $args{base_class} || 'Mouse::Object'; my $metaclass = $args{metaclass} || 'Mouse::Meta::Class'; - confess("The Metaclass $metaclass must be a subclass of Mouse::Meta::Class.") - unless $metaclass->isa('Mouse::Meta::Class'); - # make a subtype for each Mouse class Mouse::Util::TypeConstraints::class_type($class) unless Mouse::Util::TypeConstraints::find_type_constraint($class); @@ -145,62 +138,6 @@ sub init_meta { return $meta; } -sub import { - my $class = shift; - - strict->import; - warnings->import; - - my $opts = do { - if (ref($_[0]) && ref($_[0]) eq 'HASH') { - shift @_; - } else { - +{ }; - } - }; - my $level = delete $opts->{into_level}; - $level = 0 unless defined $level; - my $caller = caller($level); - - # we should never export to main - if ($caller eq 'main') { - warn qq{$class does not export its sugar to the 'main' package.\n}; - return; - } - - $class->init_meta( - for_class => $caller, - ); - - if (@_) { - __PACKAGE__->export_to_level( $level+1, $class, @_); - } else { - # shortcut for the common case of no type character - no strict 'refs'; - for my $keyword (@EXPORT) { - *{ $caller . '::' . $keyword } = *{__PACKAGE__ . '::' . $keyword}; - } - } -} - -sub unimport { - my $caller = caller; - - my $stash = do{ - no strict 'refs'; - \%{$caller . '::'} - }; - - for my $keyword (@EXPORT) { - my $code; - if(exists $is_removable{$keyword} - && ($code = $caller->can($keyword)) - && get_code_package($code) eq __PACKAGE__){ - - delete $stash->{$keyword}; - } - } -} 1; diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index 956ab99..91118cd 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -2,31 +2,29 @@ package Mouse::Role; use strict; use warnings; -use Exporter; +use Carp (); +use Scalar::Util (); -use Carp 'confess'; -use Scalar::Util 'blessed'; - -use Mouse::Util qw(load_class get_code_package not_supported); +use Mouse::Util qw(not_supported); use Mouse (); -our @ISA = qw(Exporter); - -our @EXPORT = qw( - extends with - has - before after around - override super - augment inner - - requires excludes - - blessed confess +use Mouse::Exporter; + +Mouse::Exporter->setup_import_methods( + as_is => [qw( + extends with + has + before after around + override super + augment inner + + requires excludes + ), + \&Scalar::Util::blessed, + \&Carp::confess, + ], ); -our %is_removable = map{ $_ => undef } @EXPORT; -delete $is_removable{confess}; -delete $is_removable{blessed}; sub before { my $meta = Mouse::Meta::Role->initialize(scalar caller); @@ -101,45 +99,21 @@ sub excludes { not_supported; } -sub import { - my $class = shift; +sub init_meta{ + my($class, %args) = @_; - strict->import; - warnings->import; + my $for_class = $args{for_class} + or Carp::confess("Cannot call init_meta without specifying a for_class"); - my $caller = caller; + my $metaclass = $args{metaclass} || 'Mouse::Meta::Role'; - # we should never export to main - if ($caller eq 'main') { - warn qq{$class does not export its sugar to the 'main' package.\n}; - return; - } + my $meta = $metaclass->initialize($for_class); - Mouse::Meta::Role->initialize($caller)->add_method(meta => sub { - return Mouse::Meta::Role->initialize(ref($_[0]) || $_[0]); + $meta->add_method(meta => sub{ + $metaclass->initialize(ref($_[0]) || $_[0]); }); - Mouse::Role->export_to_level(1, @_); -} - -sub unimport { - my $caller = caller; - - my $stash = do{ - no strict 'refs'; - \%{$caller . '::'} - }; - - for my $keyword (@EXPORT) { - my $code; - if(exists $is_removable{$keyword} - && ($code = $caller->can($keyword)) - && get_code_package($code) eq __PACKAGE__){ - - delete $stash->{$keyword}; - } - } - return; + return $meta; } 1;