1Return::Type(3) User Contributed Perl Documentation Return::Type(3)
2
3
4
6 Return::Type - specify a return type for a function (optionally with
7 coercion)
8
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
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 The return value is not type checked if the function is called in void
45 context.
46
47 # Note that the ~Any type is the opposite of Any.
48 # So all values will fail the type check.
49 # That means that the following function can only
50 # be called in void context.
51 #
52 sub foo :ReturnType(scalar => ~Any, list => ~Any) {
53 ...;
54 }
55
56 # Shortcut for the above.
57 #
58 sub foo :ReturnType(Void) {
59 ...;
60 }
61
62 Note that because type constraint libraries are really aimed at
63 validating scalars, the type constraint for the list is specified as a
64 hashref of numbers and not a hash of numbers! For the purposes of
65 validation against the type constraint, we slurp the returned list into
66 a temporary arrayref or hashref.
67
68 For type constraints with coercions, you can also pass the option
69 "coerce => 1":
70
71 use Return::Type;
72 use Types::Standard qw( Int Num );
73
74 # Define a subtype of "Int" at compile time, which can
75 # coerce from "Num" by rounding to nearest integer.
76 use constant Rounded => Int->plus_coercions(Num, sub { int($_) });
77
78 sub first_item :ReturnType(scalar => Rounded, coerce => 1) {
79 return $_[0];
80 }
81
82 my $answer = first_item(42, 43, 44); # returns 42
83 my $pie = first_item(3.141592); # returns 3
84
85 The options "coerce_scalar" and "coerce_list" are also available if you
86 wish to enable coercion only in particular contexts.
87
88 Power-user Inferface
89 Rather than using the ":ReturnType" attribute, it's possible to wrap a
90 coderef like this:
91
92 my $wrapped = Return::Type->wrap_sub($orig, %options);
93
94 The accepted options are "scalar", "list", "coerce", "coerce_list", and
95 "coerce_scalar", as per the attribute-based interface.
96
97 There is an additional option "scope_upper" which will load and use
98 Scope::Upper so that things like "caller" used within the wrapped sub
99 are unaware of being wrapped. This behaviour was the default prior to
100 Return::Type 0.004, but is now optional and disabled by default.
101
103 Please report any bugs to
104 <http://rt.cpan.org/Dist/Display.html?Queue=Return-Type>.
105
107 IRC: support is available through in the #moops channel on irc.perl.org
108 <http://www.irc.perl.org/channels.html>.
109
111 Attribute::Contract, Sub::Filter, Sub::Contract.
112
114 Toby Inkster <tobyink@cpan.org>.
115
117 This software is copyright (c) 2013-2014 by Toby Inkster.
118
119 This is free software; you can redistribute it and/or modify it under
120 the same terms as the Perl 5 programming language system itself.
121
123 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
124 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
125 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
126
127
128
129perl v5.36.0 2023-01-20 Return::Type(3)