From: Tokuhiro Matsuno Date: Wed, 4 Mar 2009 22:26:34 +0000 (+0000) Subject: support Mouse->import({into_level => 1}). X-Git-Tag: 0.19~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bfcc8a055b739e04da20df0069a9ad329b9429cf;hp=c4776109ad776237bc75cafd9d91abbe7c1398b7;p=gitmo%2FMouse.git support Mouse->import({into_level => 1}). This feature is needed by any::moose based hacky classes(e.g. http::engine::middleware) --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index 4731125..2281d35 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -125,7 +125,16 @@ sub import { strict->import; warnings->import; - my $caller = caller; + 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') { @@ -142,7 +151,7 @@ sub import { *{$caller.'::meta'} = sub { $meta }; if (@_) { - __PACKAGE__->export_to_level( 1, $class, @_); + __PACKAGE__->export_to_level( $level+1, $class, @_); } else { # shortcut for the common case of no type character no strict 'refs'; diff --git a/t/045-import-into_level.t b/t/045-import-into_level.t new file mode 100644 index 0000000..396d68f --- /dev/null +++ b/t/045-import-into_level.t @@ -0,0 +1,12 @@ +use strict; +use warnings; +use lib 't/lib'; +use Test::More tests => 1; + +{ + package Foo; + use BaseClass; +} + +is(Foo->new->foo(), 'bar'); + diff --git a/t/lib/BaseClass.pm b/t/lib/BaseClass.pm new file mode 100644 index 0000000..644bed1 --- /dev/null +++ b/t/lib/BaseClass.pm @@ -0,0 +1,10 @@ +package BaseClass; +use Mouse; + +sub import { + my $pkg = caller(0); + Mouse->import({into_level => 1}); + $pkg->meta->add_method('foo' => sub {'bar'}); +} + +1;