$class->meta->make_immutable(@_);
}
-my $exporter = Moose::Exporter->build_exporter(
+my $exporter = Moose::Exporter->build_import_methods(
with_caller => [
qw( extends with has before after around override augment make_immutable )
],
\&Carp::confess,
\&Scalar::Util::blessed,
],
+ also => sub { init_meta( shift, 'Moose::Object' ); },
);
-sub import {
- my $caller = Moose::Exporter->get_caller(@_);
-
- # this works because both pragmas set $^H (see perldoc perlvar)
- # which affects the current compilation - i.e. the file who use'd
- # us - which is why we don't need to do anything special to make
- # it affect that file rather than this one (which is already compiled)
-
- strict->import;
- warnings->import;
-
- # we should never export to main
- if ($caller eq 'main') {
- warn qq{Moose does not export its sugar to the 'main' package.\n};
- return;
- }
-
- init_meta($caller, 'Moose::Object');
-
- goto $exporter;
-}
-
# NOTE:
# This is for special use by
# some modules and stuff, I
# return map { $_ => $exports{$_}->() } (@_ ? @_ : keys %exports);
}
-sub unimport {
- my $caller = Moose::Exporter->get_caller(@_);
-
- Moose::Exporter->remove_keywords(
- source => __PACKAGE__,
- from => $caller,
- );
-}
-
sub init_meta {
my ( $class, $base_class, $metaclass ) = @_;
$base_class = 'Moose::Object' unless defined $base_class;
: caller($offset);
}
+sub build_import_methods {
+ my $class = shift;
+ my %args = @_;
+
+ my $exporting_package = caller();
+
+ my $exporter = $class->_build_exporter( exporting_package => $exporting_package, %args );
+
+ my $also = $args{also};
+
+ my $import = sub {
+ my $caller = Moose::Exporter->get_caller(@_);
+
+ # this works because both pragmas set $^H (see perldoc perlvar)
+ # which affects the current compilation - i.e. the file who use'd
+ # us - which is why we don't need to do anything special to make
+ # it affect that file rather than this one (which is already compiled)
+
+ strict->import;
+ warnings->import;
+
+ # we should never export to main
+ if ( $caller eq 'main' ) {
+ warn
+ qq{$exporting_package does not export its sugar to the 'main' package.\n};
+ return;
+ }
+
+ $also->($caller) if $also;
+
+ goto $exporter;
+ };
+
+ my $unimport = sub {
+ my $caller = Moose::Exporter->get_caller(@_);
+
+ Moose::Exporter->remove_keywords(
+ source => $exporting_package,
+ from => $caller,
+ );
+ };
+
+ no strict 'refs';
+ *{ $exporting_package . '::import' } = $import;
+ *{ $exporting_package . '::unimport' } = $unimport;
+}
+
my %EXPORTED;
-sub build_exporter {
+sub _build_exporter {
my $class = shift;
my %args = @_;
- my $exporting_pkg = caller();
+ my $exporting_package = $args{exporting_package};
my %exports;
for my $name ( @{ $args{with_caller} } ) {
- my $sub = do { no strict 'refs'; \&{ $exporting_pkg . '::' . $name } };
+ my $sub = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
my $wrapped = Class::MOP::subname(
- $exporting_pkg . '::' . $name => sub { $sub->( scalar caller(), @_ ) } );
+ $exporting_package . '::' . $name => sub { $sub->( scalar caller(), @_ ) } );
$exports{$name} = sub { $wrapped };
- push @{ $EXPORTED{$exporting_pkg} }, $name;
+ push @{ $EXPORTED{$exporting_package} }, $name;
}
for my $name ( @{ $args{as_is} } ) {
$name = ( Class::MOP::get_code_info($name) )[1];
}
else {
- $sub = do { no strict 'refs'; \&{ $exporting_pkg . '::' . $name } };
+ $sub = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
- push @{ $EXPORTED{$exporting_pkg} }, $name;
+ push @{ $EXPORTED{$exporting_package} }, $name;
}
$exports{$name} = sub { $sub };
croak "Moose::Role cannot support 'augment'";
}
-my $exporter = Moose::Exporter->build_exporter(
+my $exporter = Moose::Exporter->build_import_methods(
with_caller => [
qw( with requires excludes has before after around override make_immutable )
],
\&Carp::confess,
\&Scalar::Util::blessed,
],
+ also => sub { init_meta(shift) },
);
-sub import {
- my $caller = Moose::Exporter->get_caller(@_);
-
- # this works because both pragmas set $^H (see perldoc perlvar)
- # which affects the current compilation - i.e. the file who use'd
- # us - which is why we don't need to do anything special to make
- # it affect that file rather than this one (which is already compiled)
-
- strict->import;
- warnings->import;
-
- # we should never export to main
- if ($caller eq 'main') {
- warn qq{Moose::Role does not export its sugar to the 'main' package.\n};
- return;
- }
-
- init_meta($caller);
-
- goto $exporter;
-}
-
-sub unimport {
- my $caller = Moose::Exporter->get_caller(@_);
-
- Moose::Exporter->remove_keywords(
- source => __PACKAGE__,
- from => $caller,
- );
-}
-
{
my %METAS;