Dave Rolsky > Type-0.05-TRIAL > Type::Declare

Download:
Type-0.05-TRIAL.tar.gz

Dependencies

Annotate this POD

Module Version: 0.05   Source  

NAME ^

Type::Declare - Type declaration subroutines

VERSION ^

version 0.05

SYNOPSIS ^

  package MyApp::Type::Library;

  use parent 'Type::Exporter';

  use Type::Declare;
  use Type::Library::Builtins;

  declare(
      'Foo',
      parent => t('Str'),
      where  => sub { $_[0] =~ /foo/i },
  );

  declare(
      'ArrayRefOfInt',
      parent => t( 'ArrayRef', of => t('Int') ),
  );

  my $even = anon(
      parent => t('Int'),
      inline => sub {
          my $type      = shift;
          my $value_var = shift;

          return $value_var . ' % 2 == 0';
      },
  );

  coerce(
      'ArrayRef',
      from  => t('Foo'),
      using => sub { [ $_[0] ] },
  );

  coerce(
      $even,
      from  => t('Int'),
      using => sub { $_[0] % 2 ? $_[0] + 1 : $_[0] },
  );

  # Type name is DateTime
  any_isa_type('DateTime');

  # Type name is DateTimeObject
  object_isa_type( 'DateTimeObject', 'DateTime' );

  any_can_type(
      'Duck',
      methods => [ 'duck_walk', 'quack' ],
  );

  object_can_type(
      'DuckObject',
      methods => [ 'duck_walk', 'quack' ],
  );

  enum(
      'Colors',
      [qw( blue green red )],
  );

DESCRIPTION ^

This package exports a set of type declaration helpers. Importing this package also causes it to create a t() subroutine the caller.

SUBROUTINES ^

This module exports the following subroutines.

t('name')

This subroutine lets you access any types you have declared so far, as well as any types you imported from another type library.

If you pass an unknown name, it throws an exception.

declare(...)

This subroutine declares a named type. The first argument is the type name, followed by a set of key/value parameters:

anon(...)

This subroutine declares an anonymous type. It is identical to declare() except that it expects a list of key/value parameters without a type name as the first parameter.

coerce(...)

This declares a coercion from one type to another. The first argument should be an object which does the Type::Constraint::Role::Interface role. This can be either a named or anonymous type. This type is the type that the coercion is to.

The remaining arguments are key/value parameters:

DECLARATION HELPERS ^

This module also exports some helper subs for declaring certain kinds of types:

any_isa_type(), object_isa_type()

The any_isa_type() helpers creates a type which accepts a class name or object of the given class. The object_isa_type() helpers creates a type which only accepts an object of the given class.

These subroutines take a type name as the first argument. The remaining arguments are key/value pairs. Currently this is just the class key, which should be a class name. This is the class that the type requires.

The type name argument can be omitted to create an anonymous type.

any_does_type(), object_does_type()

The any_does_type() helpers creates a type which accepts a class name or object which does the given role. The object_does_type() helpers creates a type which only accepts an object which does the given role.

These subroutines take a type name as the first argument. The remaining arguments are key/value pairs. Currently this is just the role key, which should be a role name. This is the class that the type requires.

This should just work (I hope) with roles created by Moose, Mouse, and Moo (using Role::Tiny).

The type name argument can be omitted to create an anonymous type.

any_can_type(), object_can_type()

The any_can_type() helpers creates a type which accepts a class name or object with the given methods. The object_can_type() helpers creates a type which only accepts an object with the given methods.

These subroutines take a type name as the first argument. The remaining arguments are key/value pairs. Currently this is just the methods key, which can be either a string or array reference of strings. These strings are the required methods for the type.

The type name argument can be omitted to create an anonymous type.

enum()

This creates a type which accepts a string matching a given list of acceptable values.

The first argument is the type name. The remaining arguments are key/value pairs. Currently this is just the values key. This should an array reference of acceptable string values.

The type name argument can be omitted to create an anonymous type.

PARAMETERIZED TYPES ^

You can create a parameterized type by calling t() with additional parameters, like this:

  my $arrayref_of_int = t( 'ArrayRef', of => t('Int') );

  my $arrayref_of_hashref_of_int = t(
      'ArrayRef',
      of => t(
          'HashRef',
          of => t('Int'),
      ),
  );

The t() subroutine assumes that if it receives more than one argument, it should look up the named type and call $type->parameterize(...) with the additional arguments.

If the named type cannot be parameterized, it throws an error.

You can also call $type->parameterize() directly if needed. See Type::Constraint::Parameterizable for details.

AUTHOR ^

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE ^

This software is Copyright (c) 2012 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)
syntax highlighting: