Function::Return - add return type for a function
use Function::Return; use Types::Standard -types; sub foo :Return(Int) { 123 } sub bar :Return(Int) { 3.14 } foo(); # 123 bar(); # ERROR! Invalid type # multi return values sub baz :Return(Num, Str) { 3.14, 'message' } my ($pi, $msg) = baz(); my $count = baz(); # ERROR! Required list context. # empty return sub boo :Return() { return; } boo();
Function::Return allows you to specify a return type for your functions.
you can change `Return` to your own name:
use Function::Return name => 'MyReturn'; sub foo :MyReturn(Str) { }
you can switch off type check:
use Function::Return no_check => 1; sub foo :Return(Int) { 3.14 } foo(); # NO ERROR!
The function Function::Return::info lets you introspect return values like Function::Parameters::Info:
use Function::Parameters; use Function::Return; fun baz() :Return(Str) { 'hello' } my $pinfo = Function::Parameters::info \&baz; my $rinfo = Function::Return::info \&baz; $rinfo->types; # [Str]
This module supports all perl versions starting from v5.14.
It is NOT possible to specify different type constraints for scalar and list context.
Check type constraint for void context.
Function::Return::info and Function::Parameters::info can be used together.
Function::Parameters Return::Type
Copyright (C) Kenta, Kobayashi.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Kenta, Kobayashi <kentafly88@gmail.com>
To install Function::Return, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Function::Return
CPAN shell
perl -MCPAN -e shell install Function::Return
For more information on module installation, please visit the detailed CPAN module installation guide.