| MooseX::Types::Moose::MutualCoercion - Mutual coercions for common type constraints of Moose |
MooseX::Types::Moose::MutualCoercion - Mutual coercions for common type constraints of Moose
This document describes
MooseX::Types::Moose::MutualCoercion
version 0.03.
{
package Foo;
use Moose;
use MooseX::Types::Moose::MutualCoercion
qw(StrToArrayRef ArrayRefToHashKeys);
has 'thingies' =>
(is => 'rw', isa => StrToArrayRef, coerce => 1);
has 'lookup_table' =>
(is => 'rw', isa => ArrayRefToHashKeys, coerce => 1);
1;
}
my $foo = Foo->new( thingies => 'bar' );
print $foo->thingies->[0]; # 'bar'
$foo->lookup_table( [qw(baz qux)] );
print 'eureka!' # 'eureka!'
if grep {
exists $foo->lookup_table->{$_};
} qw(foo bar baz);
Much of the MooseX::Types::Moose::MutualCoercion documentation has been translated into other language(s).
MooseX::Types::Moose::MutualCoercion (This document)
This module packages several Moose::Util::TypeConstraints with coercions, designed to mutually coerce with the built-in and common types known to Moose.
NOTE: These constraints are not exported by default but you can request them in an import list like this:
use MooseX::Types::Moose::MutualCoercion qw(NumToInt ScalarRefToStr);
IntNumToInt
A subtype of Int.
If you turned coerce on, Num will become integer.
For example, 3.14 will be converted into 3.
StrScalarRefToStr
A subtype of Str.
If you turned coerce on,
ScalarRef[Str] will become dereferenced string.
For example, \do{'foo'} will be converted into foo.
ArrayRefToLines
A subtype of Str.
If you turned coerce on,
all elements of ArrayRef[Str] will be joined by $/.
For example, [qw(foo bar baz)]
will be converted into foo\nbar\nbaz\n.
NOTE: Also adds $/ to the last element.
ClassNameStrToClassName
CAVEAT: This type constraint and coercion is DEPRECATED. Please use MooseX::Types::LoadableClass's LodableClass instead of it. In addition, MooseX::Types::LoadableClass also has LodableRole.
A subtype of ClassName.
If you turned coerce on, NonEmptyStr, provided by
MooseX::Types::Common::String,
will be treated as a class name.
When it is not already loaded, it will be loaded by
Class::MOP::load_class().
ScalarRefStrToScalarRef
A subtype of ScalarRef[Str].
If you turned coerce on, Str will be referenced.
For example, foo will be converted into \do{'foo'}.
ArrayRefStrToArrayRef
A subtype of ArrayRef.
If you turned coerce on,
Str will be assigned for the first element of an array reference.
For example, foo will be converted into [qw(foo)].
LinesToArrayRef
A subtype of ArrayRef.
If you turned coerce on, Str will be split by $/
and will be assigned for each element of an array reference.
For example, foo\nbar\nbaz\n
will be converted into ["foo\n", "bar\n", "baz\n"].
NOTE: $/ was not removed.
HashRefToArrayRef
A subtype of ArrayRef.
If you turned coerce on,
HashRef will be flattened as an array reference.
For example, {foo => 0, bar => 1}
will be converted into [qw(bar 1 foo 0)].
NOTE: Order of keys/values is the same as lexically sorted keys.
HashKeysToArrayRef
A subtype of ArrayRef.
If you turned coerce on,
list of lexically sorted keys of HashRef will become an array reference.
For example, {foo => 0, bar => 1}
will be converted into [qw(bar foo)].
HashValuesToArrayRef
A subtype of ArrayRef.
If you turned coerce on,
list of values of HashRef will become an array reference.
For example, {foo => 0, bar => 1}
will be converted into [qw(1 0)].
NOTE: Order of values is the same as lexically sorted keys.
OddArrayRef
A subtype of ArrayRef, that must have odd elements.
If you turned coerce on, ArrayRef, that has even elements,
will be pushed undef as the last element.
For example, [qw(foo bar)]
will be converted into [qw(foo bar), undef].
EvenArrayRef
A subtype of ArrayRef, that must have even elements.
If you turned coerce on, ArrayRef, that has odd elements,
will be pushed undef as the last element.
For example, [qw(foo)]
will be converted into [qw(foo), undef].
HashRefArrayRefToHashRef
A subtype of HashRef.
If you turned coerce on,
all elements of EvenArrayRef will be substituted for a hash reference.
For example, [qw(foo 0 bar 1)]
will be converted into {foo => 0, bar => 1}.
ArrayRefToHashKeys
A subtype of HashRef.
If you turned coerce on,
all elements of ArrayRef will be substituted
for keys of a hash reference.
For example, [qw(foo bar baz)]
will be converted into {foo => undef, bar => undef, baz => undef}.
RegexpRefArrayRefToRegexpRef
A subtype of RegexpRef.
If you turned coerce on, all elements of ArrayRef
will be joined with | (the meta character for alternation)
and will become a regular expression reference.
For example, [qw(foo bar baz)]
will be converted into qr{foo|bar|baz}.
NOTE: If Regexp::Assemble can be loaded dynamically,
namely at runtime, a regular expression reference
will be built with this module.
For example, [qw(foo bar baz)]
will be converted into qr{(?:ba[rz]|foo)}.
About special variable $/ ($RS, $INPUT_RECORD_SEPARATOR).
perlvar
None reported.
No bugs have been reported.
Please report any found bugs, feature requests, and ideas for improvements
to <bug-moosex-types-moose-mutualcoercion at rt.cpan.org>,
or through the web interface
at http://rt.cpan.org/Public/Bug/Report.html.
I will be notified, and then you'll automatically be notified of progress
on your bugs/requests as I make changes.
When reporting bugs, if possible, please add as small a sample as you can make of the code that produces the bug. And of course, suggestions and patches are welcome.
You can find documentation for this module with the perldoc command.
% perldoc MooseX::Types::Moose::MutualCoercion
You can also find the Japanese edition of documentation for this module
with the perldocjp command from Pod::PerldocJp.
% perldocjp MooseX::Types::Moose::MutualCoercion::JA
You can also look for information at:
http://search.cpan.org/dist/MooseX-Types-Moose-MutualCoercion
http://cpanratings.perl.org/dist/MooseX-Types-Moose-MutualCoercion
This module is maintained using Git. You can get the latest version from git://github.com/gardejo/p5-moosex-types-moose-mutualcoercion.git.
More tests
<moriya at cpan dot org>,
http://gardejo.org/
Copyright (c) 2010 MORIYA Masaki, alias Gardejo
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlgpl and perlartistic.
The full text of the license can be found in the LICENSE file included with this distribution.
| MooseX::Types::Moose::MutualCoercion - Mutual coercions for common type constraints of Moose |