Object::InterfaceType - the Go lang Interface style duck type checker
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';
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';
$typename added prefix is_ is the function name, it creates to current package.
$typename
is_
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.
1
new
is_stringify
undef
$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'; }
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 ];
Kazuhiro Osawa <yappo <at> shibuya <döt> pl>
http://golang.org/doc/go_spec.html#Interface_types
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:
Non-ASCII character seen before =encoding in '<döt>'. Assuming UTF-8
To install Object::InterfaceType, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Object::InterfaceType
CPAN shell
perl -MCPAN -e shell install Object::InterfaceType
For more information on module installation, please visit the detailed CPAN module installation guide.