kura-perl
Store and export type constraints for Type::Tiny, Moose, Data::Checks, and more
When & Why to Use This Skill
kura-perl is a specialized utility designed for Perl developers to streamline type constraint management. It provides a unified interface to store and export constraints across various libraries such as Type::Tiny, Moose, and Data::Checks. By centralizing validation logic, it enhances code maintainability, ensures data integrity, and simplifies the implementation of robust type-checking in modern Perl applications, including those using the latest v5.40+ class features.
Use Cases
- Centralized Type Definition: Create a dedicated 'MyTypes' package to store and manage all application-specific type constraints in one place for better reusability.
- Modern Perl Class Validation: Implement strict attribute validation within Perl v5.40+ class structures using the ADJUST block to ensure objects are instantiated with valid data.
- Cross-Library Integration: Seamlessly bridge different Perl type systems by converting code references and library-specific constraints into a unified Type::Tiny format.
- Modular Exporting: Efficiently share specific type constraints across multiple modules using Exporter::Tiny while maintaining private constraints with underscore prefixes.
- Data Integrity Enforcement: Use 'assert_valid' and 'check' methods to perform runtime validation on complex data structures like HashRefs and nested objects.
| name | kura-perl |
|---|---|
| description | Store and export type constraints for Type::Tiny, Moose, Data::Checks, and more |
| version | 1.0.0 |
| author | kfly8 |
| tags | [perl, cpan, type-constraints, validation] |
kura - Unified Type Constraint Storage
kura provides a simple way to store and export type constraints from multiple libraries.
Core Usage
Basic Declaration
use Exporter 'import';
use Types::Common -types;
use kura Name => StrLength[1, 255];
use kura Age => Int & sub { $_ >= 0 };
use kura Email => sub { /@/ }; # Code ref auto-converted
Syntax: use kura NAME => CONSTRAINT;
Supported Constraints
- Type objects (Type::Tiny, Moose, Specio, Data::Checks)
- Code references (converted to Type::Tiny)
- Hash references with
constraintandmessage
Practical Examples
Export Types
package MyTypes {
use parent 'Exporter::Tiny';
use Types::Common -types;
use kura Name => StrLength[1, 255];
use kura Email => Str & sub { /@/ };
}
use MyTypes qw(Name Email);
Name->check('John'); # true
Built-in Class (v5.40+)
class User {
use Types::Common -types;
use kura Name => StrLength[1, 255];
field $name :param :reader;
ADJUST {
Name->assert_valid($name);
}
}
my $user = User->new(name => ''); # Dies: validation error
Best Practices
- Always load an exporter:
use Exporter 'import'; - Declare in order: Define child constraints before parent constraints
use kura Child => Str; use kura Parent => Dict[ name => Child ]; # Correct order - Private constraints: Prefix with
_to prevent exportuse kura _Private => Str; # Not exported - Package variables:
@EXPORT_OKand@KURAare auto-populated