NAME Types::LoadableClass - type constraints with coercion to load the class SYNOPSIS package MyClass; use Moose; # or Mouse, or Moo, or whatever use Types::LoadableClass qw/ LoadableClass /; has foobar_class => ( is => 'ro', required => 1, isa => LoadableClass, ); MyClass->new(foobar_class => 'FooBar'); # FooBar.pm is loaded or an # exception is thrown. DESCRIPTION A Type::Tiny-based clone of MooseX::Types::LoadableClass. This is to save yourself having to do this repeatedly... my $tc = subtype as ClassName; coerce $tc, from Str, via { Class::Load::load_class($_); $_ }; Despite the abstract for this module, `LoadableClass` doesn't actually have a coercion, so no need to use `coerce => 1` on the attribute. Rather, the class gets loaded as a side-effect of checking that it's loadable. Type Constraints `ModuleName` A subtype of `Str` (see Types::Standard) representing a string that is a valid Perl package name (according to Module::Runtime). `LoadableClass` A subtype of `ModuleName` that names a module which is either already loaded (according to Class::Load), or can be loaded (by Class::Load). `LoadableRole` A subtype of `LoadableClass` that names a module which appears to be a role rather than a class. (Because this type constraint is designed to work with Moose, Mouse, Moo, or none of the above, it can't rely on the features of any particular implementation of roles. Therefore is needs to use a heuristic to detect whether a loaded package represents a role or not. Curently this heuristic is the absence of a method named `new`.) `ClassIsa[`a]` A subtype of `LoadableClass` which checks that the class is a subclass of a given base class: ClassIsa["MyApp::Plugin"] Multiple base classes may be provided. A class only needs to satisfy one isa to pass the type constraint check. ClassIsa["MyApp::Plugin", "YourApp::Plugin"] `ClassDoes[`a]` A subtype of `LoadableClass` which checks that the class performs a given role. (This uses "DOES" in UNIVERSAL.) If multiple roles are given, the class must perform all of them. ClassDoes["MyApp::Role::Loadable", "MyApp::Role::Dumpable"] `ClassCan[`a]` A subtype of `LoadableClass` which checks that the class provides particular methods: ClassCan[ qw( new load dump ) ] Type Coercions The following named coercion can be exported: `ExpandPrefix[`a]` A coercion to expand class name abbreviations starting with a dash using a given prefix. my $type = LoadableClass->plus_coercions(ExpandPrefix["Foo"]); say $type->coerce( "-Bar" ); # Foo::Bar say $type->coerce( "Baz" ); # Baz If accepting class names from somewhere, it can be useful to provide a "default namespace" to avoid Really::Long::Package::Names::Everywhere. Here's an example of how you can do that: use strict; use warnings; use feature qw( say ); package MyApp { use Moose; use Types::LoadableClass qw( ClassDoes ExpandPrefix ); use Types::Standard qw( ArrayRef StrMatch ); my $plugin_class = ( ClassDoes["MyApp::Role::Plugin"] ) -> plus_coercions ( ExpandPrefix[ "MyApp::Plugin" ] ); has plugins => ( is => 'ro', isa => ArrayRef[ $plugin_class ], coerce => 1, ); } my $app = MyApp->new( plugins => [qw( -Foo -Bar MyApp::Baz )], ); say for @{ $app->plugins }; # MyApp::Plugin::Foo # MyApp::Plugin::Bar # MyApp::Baz BUGS Please report any bugs to . SEE ALSO Types::Standard, MooseX::Types::LoadableClass, Class::Load, Module::Runtime. AUTHOR Dagfinn Ilmari Mannsåker . Improvements, packaging, and additions by Toby Inkster . The `ClassIsa`, `ClassDoes`, and `ClassCan` types are based on suggestions by Benct Philip Jonsson. COPYRIGHT AND LICENCE This software is copyright (c) 2013-2014 by Dagfinn Ilmari Mannsåker, Toby Inkster. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. DISCLAIMER OF WARRANTIES THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.