1Return::Type(3)       User Contributed Perl Documentation      Return::Type(3)
2
3
4

NAME

6       Return::Type - specify a return type for a function (optionally with
7       coercion)
8

SYNOPSIS

10          use Return::Type;
11          use Types::Standard qw(Int);
12
13          sub first_item :ReturnType(Int) {
14             return $_[0];
15          }
16
17          my $answer = first_item(42, 43, 44);     # returns 42
18          my $pie    = first_item(3.141592);       # throws an error!
19

DESCRIPTION

21       Return::Type allows you to specify a return type for your subs. Type
22       constraints from any Type::Tiny, MooseX::Types or MouseX::Types type
23       library are supported.
24
25       The simple syntax for specifying a type constraint is shown in the
26       "SYNOPSIS". If the attribute is passed a single type constraint as
27       shown, this will be applied to the return value if called in scalar
28       context, and to each item in the returned list if called in list
29       context. (If the sub is called in void context, type constraints are
30       simply ignored.)
31
32       It is possible to specify different type constraints for scalar and
33       list context:
34
35          sub foo :ReturnType(scalar => Int, list => HashRef[Num]) {
36             if (wantarray) {
37                return (pie => 3.141592);
38             }
39             else {
40                return 42;
41             }
42          }
43
44       Note that because type constraint libraries are really aimed at
45       validating scalars, the type constraint for the list is specified as a
46       hashref of numbers and not a hash of numbers! For the purposes of
47       validation against the type constraint, we slurp the returned list into
48       a temporary arrayref or hashref.
49
50       For type constraints with coercions, you can also pass the option
51       "coerce => 1":
52
53          use Return::Type;
54          use Types::Standard qw( Int Num );
55
56          # Define a subtype of "Int" at compile time, which can
57          # coerce from "Num" by rounding to nearest integer.
58          use constant Rounded => Int->plus_coercions(Num, sub { int($_) });
59
60          sub first_item :ReturnType(scalar => Rounded, coerce => 1) {
61             return $_[0];
62          }
63
64          my $answer = first_item(42, 43, 44);     # returns 42
65          my $pie    = first_item(3.141592);       # returns 3
66
67       The options "coerce_scalar" and "coerce_list" are also available if you
68       wish to enable coercion only in particular contexts.
69
70   Power-user Inferface
71       Rather than using the ":ReturnType" attribute, it's possible to wrap a
72       coderef like this:
73
74          my $wrapped = Return::Type->wrap_sub($orig, %options);
75
76       The accepted options are "scalar", "list", "coerce", "coerce_list", and
77       "coerce_scalar", as per the attribute-based interface.
78
79       There is an additional option "scope_upper" which will load and use
80       Scope::Upper so that things like "caller" used within the wrapped sub
81       are unaware of being wrapped. This behaviour was the default prior to
82       Return::Type 0.004, but is now optional and disabled by default.
83

BUGS

85       Please report any bugs to
86       <http://rt.cpan.org/Dist/Display.html?Queue=Return-Type>.
87

SUPPORT

89       IRC: support is available through in the #moops channel on irc.perl.org
90       <http://www.irc.perl.org/channels.html>.
91

SEE ALSO

93       Attribute::Contract, Sub::Filter, Sub::Contract.
94

AUTHOR

96       Toby Inkster <tobyink@cpan.org>.
97
99       This software is copyright (c) 2013-2014 by Toby Inkster.
100
101       This is free software; you can redistribute it and/or modify it under
102       the same terms as the Perl 5 programming language system itself.
103

DISCLAIMER OF WARRANTIES

105       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
106       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
107       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
108
109
110
111perl v5.30.1                      2020-01-30                   Return::Type(3)
Impressum