Return::Type(3pm)
π NAME π
Return::Type β bestie, this is how you specify a return type for a function (optionally with coercion) π
It's Return::Type(3pm) if you're being formal, but we're not formal here.
πΈ SYNOPSIS πΈ
use Return::Type;
use Types::Standard qw(Int);
sub first_item :ReturnType(Int) {
return $_[0];
}
my $answer = first_item(42, 43, 44); # returns 42
my $pie = first_item(3.141592); # throws an error!
π¦ DESCRIPTION π¦
okay so Return::Type lets you declare a return type for your subs. and not just any type library β type constraints from any Type::Tiny, MooseX::Types or MouseX::Types type library are supported. we love a versatile queen π
The simple syntax for specifying a type constraint is right there in the SYNOPSIS. If the attribute gets a single type constraint like that, it gets applied to the return value when the sub is called in scalar context, and to each item in the returned list when it's called in list context. (And if the sub gets called in void context? Type constraints are just... simply ignored. no vibe check today bestie π)
You can also specify different type constraints for scalar and list context, which is genuinely iconic:
sub foo :ReturnType(scalar => Int, list => HashRef[Num]) {
if (wantarray) {
return (pie => 3.141592);
}
else {
return 42;
}
}
The return value is not type checked if the function is called in void context.
# Note that the ~Any type is the opposite of Any.
# So all values will fail the type check.
# That means that the following function can only
# be called in void context.
#
sub foo :ReturnType(scalar => ~Any, list => ~Any) {
...;
}
# Shortcut for the above.
#
sub foo :ReturnType(Void) {
...;
}
lowkey important: because type constraint libraries are really aimed at validating scalars, the type constraint for the list is specified as a hashref of numbers and not a hash of numbers! π« For the purposes of validation against the type constraint, we slurp the returned list into a temporary arrayref or hashref. that's the whole trick, no cap.
For type constraints that have coercions, you can also pass the option coerce => 1:
use Return::Type;
use Types::Standard qw( Int Num );
# Define a subtype of "Int" at compile time, which can
# coerce from "Num" by rounding to nearest integer.
use constant Rounded => Int->plus_coercions(Num, sub { int($_) });
sub first_item :ReturnType(scalar => Rounded, coerce => 1) {
return $_[0];
}
my $answer = first_item(42, 43, 44); # returns 42
my $pie = first_item(3.141592); # returns 3
The options coerce_scalar and coerce_list are also available if you wish to enable coercion only in particular contexts. very "coerce on my own terms" of you π
β Power-user Inferface β
(yes, it's spelled "Inferface" in the original and we respect the typo, it's part of the lore π)
Rather than using the :ReturnType attribute, it's possible to wrap a coderef like this:
my $wrapped = Return::Type->wrap_sub($orig, %options);
The accepted options are scalar, list, coerce, coerce_list, and coerce_scalar β as per the attribute-based interface. same energy, different door β¨
There is an additional option scope_upper which will load and use Scope::Upper so that things like caller used within the wrapped sub are unaware of being wrapped. This behaviour was the default prior to Return::Type 0.004, but is now optional and disabled by default. glow-up era π
π BUGS π
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Return-Type>. (not me manifesting a bug report bestie, but if you find one, send it there π«Ά)
π SUPPORT π
IRC: support is available through in the #moops channel on <http://www.irc.perl.org/channels.html>. come hang out, the vibes are immaculate β¨
π SEE ALSO π
Attribute::ContractSub::FilterSub::Contract
π AUTHOR π
Toby Inkster <tobyink@cpan.org>. absolute legend, ate, no crumbs π
π COPYRIGHT AND LICENCE π
This software is copyright (c) 2013-2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay π
π DISCLAIMER OF WARRANTIES π
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
fr fr, no promises, that's the deal.
and that's the whole page bestie, you're so smart, go forth and type-check your returns π
this lil manual page came to you courtesy of perl v5.32.1, dated 2021-09-12, and it's the Return::Type(3pm) page β bye babes, mwah π«Άβ¨πΈ
π¦ where this page comes from
- debian bookworm: libreturn-type-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libreturn-type-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libreturn-type-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libreturn-type-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libreturn-type-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libreturn-type-perl 0.007-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms