NAME

Data::Sah::Util::Type - Utility functions related to types

VERSION

This document describes version 0.46 of Data::Sah::Util::Type (from Perl distribution Data-Sah-Util-Type), released on 2016-12-09.

SYNOPSIS

use Data::Sah::Util::Type qw(
    get_type
    is_type
    is_simple is_numeric is_collection is_ref
);

say get_type("int");                          # -> int
say get_type("int*");                         # -> int
say get_type([int => min=>0]);                # -> int
say get_type("foo");                          # -> foo (doesn't check type is known)

say is_type("int*");                          # -> 1
say is_type("foo");                           # -> 0

say is_simple("int");                          # -> 1
say is_simple("array");                        # -> 0
say is_simple([any => of => ["float", "str"]); # -> 1
say is_simple("re");                           # -> 1
say is_simple("foo");                          # -> 0

say is_collection("array*");            # -> 1
say is_collection(["hash", of=>"int"]); # -> 1
say is_collection("str");               # -> 0
say is_collection("foo");               # -> 0

say is_ref("code*"); # -> 1
say is_ref("array"); # -> 1
say is_ref("str");   # -> 0
say is_ref("foo");   # -> 0

say is_numeric(["int", min=>0]); # -> 1
say is_numeric("str");           # -> 0
say is_numeric("foo");           # -> 0

DESCRIPTION

This module provides some secondary utility functions related to Sah and Data::Sah. It is deliberately distributed separately from the Data-Sah main distribution to be differentiated from Data::Sah::Util which contains "primary" utilities and is distributed with Data-Sah.

Reference table for simple/collection/ref/numeric criteria of builtin types:

+----------+-----------+---------------+--------+------------+
| type     | is_simple | is_collection | is_ref | is_numeric |
+----------+-----------+---------------+--------+------------+
| array    |           | 1             | 1      |            |
| bool     | 1         |               |        |            |
| buf      | 1         |               |        |            |
| cistr    | 1         |               |        |            |
| code     |           |               | 1      |            |
| date     | 1         |               |        |            |
| duration | 1         |               |        |            |
| float    | 1         |               |        | 1          |
| hash     |           | 1             | 1      |            |
| int      | 1         |               |        | 1          |
| num      | 1         |               |        | 1          |
| obj      |           |               | 1      |            |
| re       | 1         |               | 1      |            |
| str      | 1         |               |        |            |
| undef    | 1         |               |        |            |
+----------+-----------+---------------+--------+------------+

FUNCTIONS

None exported by default, but they are exportable.

get_type($sch) => STR

Return type name.

is_type($sch) => STR

Return type name if type in schema is known, or undef.

is_simple($sch[, \%opts]) => BOOL

Simple means "scalar" or can be represented as a scalar. This is currently used to determine if a builtin type can be specified as an argument or option value in command-line.

This includes re, bool, as well as date and duration.

If type is all, then for this routine to be true all of the mentioned types must be simple. If type is any, then for this routine to be true at least one of the mentioned types must be simple.

Options:

  • schema_is_normalized => BOOL

is_collection($sch[, \%opts]) => BOOL

Collection means array or hash.

If type is all, then for this routine to be true all of the mentioned types must be collection. If type is any, then for this routine to be true at least one of the mentioned types must be collection.

is_ref($sch[, \%opts]) => BOOL

"Ref" means generally a reference in Perl. But date and duration are not regarded as "ref". Regular expression on the other hand is regarded as a ref.

If type is all, then for this routine to be true all of the mentioned types must be "ref". If type is any, then for this routine to be true at least one of the mentioned types must be "ref".

is_numeric($sch[, \%opts]) => BOOL

Currently, only num, int, and float are numeric.

If type is all, then for this routine to be true all of the mentioned types must be numeric. If type is any, then for this routine to be true at least one of the mentioned types must be numeric.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Data-Sah-Util-Type.

SOURCE

Source repository is at https://github.com/perlancar/perl-Data-Sah-Util-Type.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Util-Type

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

Data::Sah

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.