Kazuhiro Osawa > Object-InterfaceType-0.01 > Object::InterfaceType

Download:
Object-InterfaceType-0.01.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.01   Source  

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.

syntax highlighting: