Moose::Exporter - make an import just like Moose.pm


NAME

Moose::Exporter - make an import() and unimport() just like Moose.pm


SYNOPSIS

  package MyApp::Moose;
  use Moose ();
  use Moose::Exporter;
  Moose::Exporter->setup_import_methods(
      with_caller => [ 'has_rw', 'sugar2' ],
      as_is       => [ 'sugar3', \&Some::Random::thing ],
      also        => 'Moose',
  );
  sub has_rw {
      my ($caller, $name, %options) = @_;
      Class::MOP::class_of($caller)->add_attribute($name,
          is => 'rw',
          %options,
      );
  }
  # then later ...
  package MyApp::User;
  use MyApp::Moose;
  has 'name';
  has_rw 'size';
  thing;
  no MyApp::Moose;


DESCRIPTION

This module encapsulates the exporting of sugar functions in a Moose.pm-like manner. It does this by building custom import and unimport methods for your module, based on a spec you provide.

It also lets you "stack" Moose-alike modules so you can export Moose's sugar as well as your own, along with sugar from any random MooseX module, as long as they all use Moose::Exporter.

To simplify writing exporter modules, Moose::Exporter also imports strict and warnings into your exporter module, as well as into modules that use it.


METHODS

This module provides two public methods:

Moose::Exporter-setup_import_methods(...) >>

When you call this method, Moose::Exporter build custom import and unimport methods for your module. The import method will export the functions you specify, and you can also tell it to export functions exported by some other module (like Moose.pm).

The unimport method cleans the callers namespace of all the exported functions.

This method accepts the following parameters:

Moose::Exporter-build_import_methods(...) >>

Returns two code refs, one for import and one for unimport.

Used by setup_import_methods.


IMPORTING AND init_meta

If you want to set an alternative base object class or metaclass class, simply define an init_meta method in your class. The import method that Moose::Exporter generates for you will call this method (if it exists). It will always pass the caller to this method via the for_class parameter.

Most of the time, your init_meta method will probably just call < Moose-init_meta >> to do the real work:

  sub init_meta {
      shift; # our class name
      return Moose->init_meta( @_, metaclass => 'My::Metaclass' );
  }


METACLASS TRAITS

The import method generated by Moose::Exporter will allow the user of your module to specify metaclass traits in a -traits parameter passed as part of the import:

  use Moose -traits => 'My::Meta::Trait';
  use Moose -traits => [ 'My::Meta::Trait', 'My::Other::Trait' ];

These traits will be applied to the caller's metaclass instance. Providing traits for an exporting class that does not create a metaclass for the caller is an error.


AUTHOR

Dave Rolsky <autarch@urth.org>

This is largely a reworking of code in Moose.pm originally written by Stevan Little and others.


COPYRIGHT AND LICENSE

Copyright 2009 by Infinity Interactive, Inc.

http://www.iinteractive.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

 Moose::Exporter - make an import just like Moose.pm