The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

SYNOPSIS

  package MyApp::Moose;

  use strict;
  use warnings;

  use Moose ();
  use Moose::Exporter;

  Moose::Exporter->setup_import_methods(
      with_caller => [ 'sugar1', 'sugar2' ],
      as_is       => [ 'sugar3', \&Some::Random::thing ],
      also        => 'Moose',
  );

  # then later ...
  package MyApp::User;

  use MyApp::Moose;

  has 'name';
  sugar1 'do your thing';
  thing;

  no MyApp::Moose;

DESCRIPTION

This module encapsulates the logic to export sugar functions like Moose.pm. It does this by building custom import and unimport methods for your module, based on a spec your provide.

It also lets your "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.

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:

  • with_caller => [ ... ]

    This a list of function names only to be exported wrapped and then exported. The wrapper will pass the name of the calling package as the first argument to the function. Many sugar functions need to know their caller so they can get the calling package's metaclass object.

  • as_is => [ ... ]

    This a list of function names or sub references to be exported as-is. You can identify a subroutine by reference, which is handy to re-export some other module's functions directly by reference (\&Some::Package::function).

  • also => $name or \@names

    This is a list of modules which contain functions that the caller wants to export. These modules must also use Moose::Exporter. The most common use case will be to export the functions from Moose.pm.

    Moose::Exporter also makes sure all these functions get removed when unimport is called.

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 2008 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.