28 Apr 2009 00:06:04 UTC
- Distribution: MooseX-Lexical-Types
- Module version: 0.01
- Source (raw)
- Browse (raw)
- How to Contribute
- Testers (838 / 3 / 1)
- KwaliteeBus factor: 2
- % Coverage
- License: perl_5
- Activity24 month
- Download (9.73KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest versionFLORA Florian Ragwitz
MooseX::Lexical::Types - automatically validate lexicals against Moose type constraints
use MooseX::Types::Moose qw/Int/; # import Int type constraint use MooseX::Lexical::Types qw/Int/; # register Int constraint as lexical type my Int $foo; # declare typed variable $foo = 42; # works $foo = 'bar'; # fails
This module allows you to automatically validate values assigned to lexical variables using Moose type constraints.
This can be done by importing all the MooseX::Types constraints you you need into your namespace and then registering them with MooseX::Lexical::Types. After that the type names may be used in declarations of lexical variables via
Values assigned to variables declared with type constraints will be checked against the type constraint.
At runtime the type exports will still return
There are a couple of caveats:
- It only works with imported MooseX::Types
Using normal strings as type constraints, like allowed in declaring type constraints for attributes with Moose, doesn't work.
- It only works with scalars
my Str @foowill not work.
- It only works with simple named types
The type name specified after
myneeds to be a simple bareword. Things like
my ArrayRef[Str] $foowill not work. You will need to declare a named for every type you want to use in
subtype ArrayOfStr, as ArrayRef[Str]; my ArrayofStr $foo;
- Values are only validated on assignment
my Int $foo;
will not fail, even if
$foonow holds the value
undef, which wouldn't validate against
Int. In the future this module might also validate the value on the first fetch from the variable to properly fail when using an uninitialized variable with a value that doesn't validate.
Florian Ragwitz <email@example.com>
This software is copyright (c) 2009 by Florian Ragwitz.
This is free software; you can redistribute it and/or modify it under the same terms as perl itself.
Module Install Instructions
To install MooseX::Lexical::Types, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install MooseX::Lexical::Types
For more information on module installation, please visit the detailed CPAN module installation guide.