NAME
MooseX::Types::StrictScalarTypes - strict Moose type constraints for integers, numbers, and Booleans
SYNOPSIS
package Foo;
use Moose;
use MooseX::Types::StrictScalarTypes qw(StrictInt StrictNumber StrictBool);
has integer => ( is => 'ro', isa => StrictInt );
has number => ( is => 'ro', isa => StrictNumber );
has boolean => ( is => 'ro', isa => StrictBool );
These will all throw exceptions:
Foo->new(integer => 3.14); # because 3.14 is a float, not an int
Foo->new(number => "3.14"); # that's a string, not a number
Foo->new(boolean => 1); # that's an integer, not the result of a comparison
And these will not:
Foo->new(integer => 3);
Foo->new(number => 3.14);
Foo->new(boolean => 0 == 1);
DESCRIPTION
Perl scalars can be either strings or numbers, and normally you don't really care which is which as it will do all the necessary type conversions automagically.
But in some rare cases the difference matters. This package provides some Moose type constraints to let you easily enforce stricter type checking than what Moose can normally do with its builtin Int
, Number
and Bool
types.
Internally it uses Scalar::Type so see its documentation if you want to know the gory details.
LIMITATIONS
The Boolean type is only available in perl 5.36 and higher. Attempting to use it on an older perl is a fatal error. You can use Scalar::Type::bool_supported
to easily check at run-time whether it will be available or not.
TYPES
- StrictInt
-
A type that only accepts integers. Floating point numbers such as
3.0
and strings like"3"
are not permitted. - StrictNumber
-
A type that only accepts numbers. Strings like
"3"
are not permitted. - StrictBool
-
A type that only accepts Booleans, the result of a comparison. Numbers and strings such as
1
and""
are not permitted, but values like1 == 0
are.
SEE ALSO
Test2::Tools::Type - similarly strict checking for your tests
BUGS
If you find any bugs please report them on Github, preferably with a test case.
FEEDBACK
I welcome feedback about my code, especially constructive criticism.
AUTHOR, COPYRIGHT and LICENCE
Copyright 2024 David Cantrell <david@cantrell.org.uk>
This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
CONSPIRACY
This module is also free-as-in-mason software.