The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Raisin::Types::Base - Base class for Raisin::Types.

SYNOPSIS

    package Raisin::Types::Integer;
    use base 'Raisin::Types::Base';

    sub constraint {
        my ($self, $v) = @_;
        length($v) <= 10 ? 1 : 0
    }
    sub coercion {
        my ($self, $v) = @_; # REF
        $$v = sprintf 'INT:%d', $$v;
    }

    package main;

    use Raisin::Types::Integer;

    say Raisin::Types::Integer->new(1234) ? 'valid' : 'invalid';
    say Raisin::Types::Integer->new(10.1) ? 'valid' : 'invalid';

DESCRIPTION

Base class for each Raisin type.

Contains three base methods: name, constraint and coercion.

METHODS

name

Return type's name.

By default will be returned last string after after ::. If you want customize variable name you can redefine name subroutine.

    sub name { 'FancyTypeName' }

constraint

    sub constraint {
        my ($self, $v) = @_;
        length($v) <= 10 ? 1 : 0
    }

coercion

    sub coercion {
        my ($self, $v) = @_; # REF
        $$v = sprintf 'INT:%d', $$v;
    }