NAME

Type::Guess - Infer data types from an array of scalars

SYNOPSIS

use Type::Guess;

my @list = qw/a b cd efg hijk/;
my $guess = Type::Guess->new(@list);

print $guess->type;       # Str
print $guess->precision;  # 0
print $guess->length;     # 4
print $guess->to_string;  # %-7s

DESCRIPTION

Type::Guess analyzes an array of scalar values and determines the most appropriate data type. It provides type inference, precision estimation, and formatting options.

METHODS

new

my $guess = Type::Guess->new(@list);

Creates a new Type::Guess object based on the input list.

type

my $type = $guess->type;

Returns the inferred type. Possible values include:

  • Str (String)

  • Int (Integer)

  • Num (Floating point number)

  • Other types based on roles

precision

my $precision = $guess->precision;

Returns the detected precision for numeric values. Defaults to 0 for integers.

length

my $length = $guess->length;

Returns the length of the longest string representation in the list.

integer_chars

my $int_chars = $guess->integer_chars;

Returns the count of integer digits in numeric types.

signed

my $signed = $guess->signed;

Returns true if negative values are detected.

to_string

my $format = $guess->to_string;

Returns a format string suitable for printf-style formatting.

sql

my $sql_type = $guess->sql;

Returns the SQL equivalent type, such as "integer" or "float".

precision($new_precision)

$guess->precision(2);

Sets the precision for floating point numbers.

type($new_type)

$guess->type("Str");

Forces a new type assignment.

ROLES

Type::Guess supports additional roles for specialized type handling:

+Date

my $guess = Type::Guess->with_roles("+Date")->new(@dates);

Enables date recognition for values like YYYY-MM-DD.

+Unicode

my $guess = Type::Guess->with_roles("+Unicode")->new(@unicode_strings);

Handles multibyte characters correctly in formatting.

+Tiny

my $guess = Type::Guess->with_roles("+Tiny")->new(@list);

Integrates with Type::Tiny for strict type validation.

CONFIGURATION

tolerance

Type::Guess->tolerance(0.1);

Sets tolerance for non-compliant data.

Type::Guess->tolerance(0.25);

my $t = Type::Guess->new(1, 2, 3, "a");
# Int

Type::Guess->tolerance(0);

my $t = Type::Guess->new(1, 2, 3, "a");
# Str

skip_empty

Type::Guess->skip_empty(1);

When false, all values - empty or not - are checked. Defaults to true.

Type::Guess->skip_empty(1);
$t = Type::Guess->new(1, 2, "", 3, 4);
# int

Type::Guess->skip_empty(0);
$t = Type::Guess->new(1, 2, "", 3, 4);
# Snt

AUTHOR

Your Name <scesano@cpan.org>

LICENSE

This module is licensed under the same terms as Perl itself.