The Perl Advent Calendar needs more articles for 2022. Submit your idea today!

NAME

Venus::Type - Type Class

ABSTRACT

Type Class for Perl 5

SYNOPSIS

  package main;

  use Venus::Type;

  my $type = Venus::Type->new([]);

  # $type->code;

DESCRIPTION

This package provides methods for casting native data types to objects and the reverse.

INHERITS

This package inherits behaviors from:

Venus::Kind::Utility

INTEGRATES

This package integrates behaviors from:

Venus::Role::Accessible

Venus::Role::Buildable

Venus::Role::Valuable

METHODS

This package provides the following methods:

code

  code() (Str | Undef)

The code method returns the name of the value's data type.

Since 0.01

code example 1
  # given: synopsis;

  my $code = $type->code;

  # "ARRAY"
code example 2
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => {});

  my $code = $type->code;

  # "HASH"
code example 3
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => qr//);

  my $code = $type->code;

  # "REGEXP"

coded

  coded(Str $code) (Bool)

The coded method return true or false if the data type name provided matches the result of "code".

Since 1.23

coded example 1
  # given: synopsis;

  my $coded = $type->coded('ARRAY');

  # 1
coded example 2
  # given: synopsis;

  my $coded = $type->coded('HASH');

  # 0

deduce

  deduce() (Object)

The deduce methods returns the argument as a data type object.

Since 0.01

deduce example 1
  # given: synopsis;

  my $deduce = $type->deduce;

  # bless({ value => [] }, "Venus::Array")
deduce example 2
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => {});

  my $deduce = $type->deduce;

  # bless({ value => {} }, "Venus::Hash")
deduce example 3
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => qr//);

  my $deduce = $type->deduce;

  # bless({ value => qr// }, "Venus::Regexp")
deduce example 4
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => '1.23');

  my $deduce = $type->deduce;

  # bless({ value => "1.23" }, "Venus::Float")

deduce_deep

  deduce_deep() (Object)

The deduce_deep function returns any arguments as data type objects, including nested data.

Since 0.01

deduce_deep example 1
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => [1..4]);

  my $deduce_deep = $type->deduce_deep;

  # bless({
  #   value => [
  #     bless({ value => 1 }, "Venus::Number"),
  #     bless({ value => 2 }, "Venus::Number"),
  #     bless({ value => 3 }, "Venus::Number"),
  #     bless({ value => 4 }, "Venus::Number"),
  #   ],
  # }, "Venus::Array")
deduce_deep example 2
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => {1..4});

  my $deduce_deep = $type->deduce_deep;

  # bless({
  #   value => {
  #     1 => bless({ value => 2 }, "Venus::Number"),
  #     3 => bless({ value => 4 }, "Venus::Number"),
  #   },
  # }, "Venus::Hash")

detract

  detract() (Any)

The detract method returns the argument as native Perl data type value.

Since 0.01

detract example 1
  package main;

  use Venus::Type;
  use Venus::Hash;

  my $type = Venus::Type->new(Venus::Hash->new({1..4}));

  my $detract = $type->detract;

  # { 1 => 2, 3 => 4 }
detract example 2
  package main;

  use Venus::Type;
  use Venus::Array;

  my $type = Venus::Type->new(Venus::Array->new([1..4]));

  my $detract = $type->detract;

  # [1..4]
detract example 3
  package main;

  use Venus::Type;
  use Venus::Regexp;

  my $type = Venus::Type->new(Venus::Regexp->new(qr/\w+/));

  my $detract = $type->detract;

  # qr/\w+/
detract example 4
  package main;

  use Venus::Type;
  use Venus::Float;

  my $type = Venus::Type->new(Venus::Float->new('1.23'));

  my $detract = $type->detract;

  # "1.23"

detract_deep

  detract_deep() (Any)

The detract_deep method returns any arguments as native Perl data type values, including nested data.

Since 0.01

detract_deep example 1
  package main;

  use Venus::Type;
  use Venus::Hash;

  my $type = Venus::Type->new(Venus::Hash->new({1..4}));

  my $detract_deep = Venus::Type->new($type->deduce_deep)->detract_deep;

  # { 1 => 2, 3 => 4 }
detract_deep example 2
  package main;

  use Venus::Type;
  use Venus::Array;

  my $type = Venus::Type->new(Venus::Array->new([1..4]));

  my $detract_deep = Venus::Type->new($type->deduce_deep)->detract_deep;

  # [1..4]

identify

  identify() (Bool, Bool, Str)

The identify method returns the value's data type, or "code", in scalar context. In list context, this method will return a tuple with (defined, blessed, and data type) elements. Note: For globs and file handles this method will return "scalar" as the data type.

Since 1.23

identify example 1
  # given: synopsis

  package main;

  my ($defined, $blessed, $typename) = $type->identify;

  # (1, 0, 'ARRAY')
identify example 2
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => {});

  my ($defined, $blessed, $typename) = $type->identify;

  # (1, 0, 'HASH')
identify example 3
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => qr//);

  my ($defined, $blessed, $typename) = $type->identify;

  # (1, 1, 'REGEXP')
identify example 4
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => bless{});

  my ($defined, $blessed, $typename) = $type->identify;

  # (1, 1, 'OBJECT')

package

  package() (Str)

The code method returns the package name of the objectified value, i.e. ref().

Since 0.01

package example 1
  # given: synopsis;

  my $package = $type->package;

  # "Venus::Array"
package example 2
  package main;

  use Venus::Type;

  my $type = Venus::Type->new(value => {});

  my $package = $type->package;

  # "Venus::Hash"