The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 114:

Non-ASCII character seen before =encoding in '<döt>'. Assuming UTF-8