|
NAMEScalar::Does - like ref() but useful SYNOPSIS use Scalar::Does qw( -constants );
my $object = bless {}, 'Some::Class';
does($object, 'Some::Class'); # true
does($object, '%{}'); # true
does($object, HASH); # true
does($object, ARRAY); # false
DESCRIPTIONIt has long been noted that Perl would benefit from a does() built-in. A check that "ref($thing) eq 'ARRAY'" doesn't allow you to accept an object that uses overloading to provide an array-like interface. Functions
If the given role is blessed, and provides a "check" method, then "does" delegates to that. Otherwise, if the scalar being tested is blessed, then "$scalar->DOES($role)" is called, and "does" returns true if the method call returned true. If the scalar being tested looks like a Perl class name, then "$scalar->DOES($role)" is also called, and the string "0E0" is returned for success, which evaluates to 0 in a numeric context but true in a boolean context.
ConstantsThe following constants may be exported for convenience:
ExportBy default, only "does" is exported. This module uses Exporter::Tiny, so functions can be renamed: use Scalar::Does does => { -as => 'performs_role' };
Scalar::Does also plays some tricks with namespace::clean to ensure that any functions it exports to your namespace are cleaned up when you're finished with them. This ensures that if you're writing object-oriented code "does" and "overloads" will not be left hanging around as methods of your classes. Moose::Object provides a "does" method, and you should be able to use Scalar::Does without interfering with that. You can import the constants (plus "does") using: use Scalar::Does -constants; The "make_role" and "where" functions can be exported like this: use Scalar::Does -make; Or list specific functions/constants that you wish to import: use Scalar::Does qw( does ARRAY HASH STRING NUMBER ); Custom Role Checks use Scalar::Does
custom => { -as => 'does_array', -role => 'ARRAY' },
custom => { -as => 'does_hash', -role => 'HASH' };
does_array($thing);
does_hash($thing);
BUGSPlease report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Scalar-Does>. SEE ALSOScalar::Util. <http://perldoc.perl.org/5.10.0/perltodo.html#A-does()-built-in>. Relationship to Moose rolesScalar::Does is not dependent on Moose, and its role-checking is not specific to Moose's idea of roles, but it does work well with Moose roles. Moose::Object overrides "DOES", so Moose objects and Moose roles should "just work" with Scalar::Does. {
package Transport;
use Moose::Role;
}
{
package Train;
use Moose;
with qw(Transport);
}
my $thomas = Train->new;
does($thomas, 'Train'); # true
does($thomas, 'Transport'); # true
does($thomas, Transport->meta); # not yet supported!
Mouse::Object should be compatible enough to work as well. See also: Moose::Role, Moose::Object, UNIVERSAL. Relationship to Moose type constraintsMoose::Meta::TypeConstraint objects, plus the constants exported by MooseX::Types libraries all provide a "check" method, so again, should "just work" with Scalar::Does. Type constraint strings are not supported however. use Moose::Util::TypeConstraints qw(find_type_constraint);
use MooseX::Types qw(Int);
use Scalar::Does qw(does);
my $int = find_type_constraint("Int");
does( "123", $int ); # true
does( "123", Int ); # true
does( "123", "Int" ); # false
Mouse::Meta::TypeConstraints and MouseX::Types should be compatible enough to work as well. See also: Moose::Meta::TypeConstraint, Moose::Util::TypeConstraints, MooseX::Types, Scalar::Does::MooseTypes. Relationship to Type::Tiny type constraintsTypes built with Type::Tiny and Type::Library can be used exactly as Moose type constraint objects above. use Types::Standard qw(Int); use Scalar::Does qw(does); does(123, Int); # true In fact, Type::Tiny and related libraries are used extensively in the internals of Scalar::Does 0.200+. See also: Type::Tiny, Types::Standard. Relationship to Role::Tiny and Moo rolesRoles using Role::Tiny 1.002000 and above provide a "DOES" method, so should work with Scalar::Does just like Moose roles. Prior to that release, Role::Tiny did not provide "DOES". Moo's role system is based on Role::Tiny. See also: Role::Tiny, Moo::Role. AUTHORToby Inkster <tobyink@cpan.org>. COPYRIGHT AND LICENCEThis software is copyright (c) 2012-2014, 2017 by 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 WARRANTIESTHIS 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.
|