No description, website, or topics provided.
Perl
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/Object
t
xt
.gitignore
.shipit
Changes
MANIFEST
MANIFEST.SKIP
Makefile.PL
README
README.pod

README.pod

NAME

Object::InterfaceType - the Go lang Interface style duck type checker

SYNOPSIS

use Object::InterfaceType;

# create interface type check function
interface_type stringify => ['as_string'];
is_stringify(URI->new) ? 'ok' : 'ng';

# get interface type code reference
my $is_stringify = interface_type ['as_string'];
$is_stringify->(URI->new) ? 'ok' : 'ng';

DESCRIPTION

Object::InterfaceType is Go lang Interface style duck type checker.

This module export is interface_type function.

It is useful when you receive an object with a specific method. you can the recyclable duck type check can be performed.

Object::InterfaceType is using Exporter, export of interface_type can be controlled.

use Object::InterfaceType ();
Object::InterfaceType::interface_type stringify => ['as_string'];
is_stringify(URI->new) ? 'ok' : 'ng';

FUNCTION SPEC

interface_type $typename => \@methods

$typename added prefix is_ is the function name, it creates to current package.

interface_type stringify => [qw/ new as_string /];

This created function is used as follows.

my $uri = URI->new;
if (is_stringify($uri)) {
    say '$uri is stringify object';
}

1 is returned by an object with new and is_stringify method. undef returns in the other object.

my $check_code_reference = interface_type \@methods

$typename is omissible. in that case, the reference of the code to check is returned, without performing creation of a function.

my $is_stringify = interface_type [qw/ new as_string /];

my $uri = URI->new;
if ($is_stringify->($uri)) {
    say '$uri is stringify object';
}

TODO

includable interface is unsupported.

specification proposal

interface_type stringify => [qw/ as_string /];
my $is_dump = interface_type [qw/ as_dump /];

# including stringify and $is_dump
interface_type dump_with_syringify => [
    'new', \&is_stringify, $is_dump
];

AUTHOR

Kazuhiro Osawa <yappo <at> shibuya <döt> pl>

SEE ALSO

http://golang.org/doc/go_spec.html#Interface_types

LICENSE

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