{"id":16307575,"url":"https://github.com/kfly8/p5-function-return","last_synced_at":"2025-09-30T01:32:01.870Z","repository":{"id":47340810,"uuid":"146607950","full_name":"kfly8/p5-Function-Return","owner":"kfly8","description":"specify a function return type","archived":false,"fork":false,"pushed_at":"2023-03-26T05:31:15.000Z","size":92,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-06-19T04:21:41.071Z","etag":null,"topics":["perl","perl5","perl5-module"],"latest_commit_sha":null,"homepage":"","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kfly8.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-29T13:57:23.000Z","updated_at":"2023-04-14T23:43:36.000Z","dependencies_parsed_at":"2024-06-19T04:08:28.499Z","dependency_job_id":"3f247d73-7ee0-4037-ac65-25496f932ac5","html_url":"https://github.com/kfly8/p5-Function-Return","commit_stats":{"total_commits":81,"total_committers":4,"mean_commits":20.25,"dds":"0.24691358024691357","last_synced_commit":"5e95a28eb5803ad571f312d360ff3ce73441d3e2"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-Function-Return","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-Function-Return/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-Function-Return/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-Function-Return/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kfly8","download_url":"https://codeload.github.com/kfly8/p5-Function-Return/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219865428,"owners_count":16555929,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["perl","perl5","perl5-module"],"created_at":"2024-10-10T21:14:37.285Z","updated_at":"2025-09-30T01:31:56.550Z","avatar_url":"https://github.com/kfly8.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Actions Status](https://github.com/kfly8/p5-Function-Return/workflows/test/badge.svg)](https://github.com/kfly8/p5-Function-Return/actions) [![Coverage Status](https://img.shields.io/coveralls/kfly8/p5-Function-Return/main.svg?style=flat)](https://coveralls.io/r/kfly8/p5-Function-Return?branch=main) [![MetaCPAN Release](https://badge.fury.io/pl/Function-Return.svg)](https://metacpan.org/release/Function-Return)\n# NAME\n\nFunction::Return - specify a function return type\n\n# SYNOPSIS\n\n```perl\nuse Function::Return;\nuse Types::Standard -types;\n\nsub foo :Return(Int) { 123 }\nsub bar :Return(Int) { 3.14 }\n\nfoo(); # 123\nbar(); # ERROR! Invalid type\n\n# multi return values\nsub baz :Return(Num, Str) { 3.14, 'message' }\nmy ($pi, $msg) = baz();\nmy $count = baz(); # ERROR! Required list context.\n\n# empty return\nsub boo :Return() { return; }\nboo();\n```\n\n# DESCRIPTION\n\nFunction::Return allows you to specify a return type for your functions.\n\n## SUPPORT\n\nThis module supports all perl versions starting from v5.14.\n\n## IMPORT OPTIONS\n\n### no\\_check\n\nYou can switch off type check.\nIf you change globally, use `\u003c$ENV{FUNCTION_RETURN_NO_CHECK}`\u003e:\n\n```perl\nBEGIN {\n    $ENV{FUNCTION_RETURN_NO_CHECK} = 1;\n}\nuse Function::Return;\nsub foo :Return(Int) { 3.14 }\nfoo(); # NO ERROR!\n```\n\nAnd If you want to switch by a package, it is better to use the no\\_check option:\n\n```perl\nuse Function::Return no_check =\u003e 1;\nsub foo :Return(Int) { 3.14 }\nfoo(); # NO ERROR!\n```\n\n### pkg\n\nFunction::Return automatically exports a return type by caller.\n\nOr you can specify a package name:\n\n```perl\nuse Function::Return pkg =\u003e 'MyClass';\n```\n\n## ATTRIBUTES\n\n### Return\n\n`:Return` attribute is available.\n\n## FUNCTIONS\n\n### meta\n\nThis function lets you introspect return values:\n\n```perl\nuse Function::Return;\nuse Types::Standard -types;\n\nsub baz() :Return(Str) { 'hello' }\n\nmy $meta = Function::Return::meta \\\u0026baz; # Sub::Meta\n$meta-\u003ereturns-\u003elist; # [Str]\n```\n\nIn addition, it can be used with [Function::Parameters](https://metacpan.org/pod/Function%3A%3AParameters):\n\n```perl\nuse Function::Parameters;\nuse Function::Return;\nuse Types::Standard -types;\n\nfun hello(Str $msg) :Return(Str) { 'hello' . $msg }\n\nmy $meta = Function::Return::meta \\\u0026hello; # Sub::Meta\n$meta-\u003ereturns-\u003elist; # [Str]\n\n$meta-\u003eargs-\u003e[0]-\u003etype; # Str\n$meta-\u003eargs-\u003e[0]-\u003ename; # $msg\n\n# Note\nFunction::Parameters::info \\\u0026hello; # undef\n```\n\nThis makes it possible to know both type information of function arguments and return value at compile time, making it easier to use for testing etc.\n\n## METHODS\n\n### wrap\\_sub($coderef)\n\nThis interface is for power-user. Rather than using the `:Return` attribute, it's possible to wrap a coderef like this:\n\n```perl\nmy $wrapped = Function::Return-\u003ewrap_sub($orig, [Str]);\n$wrapped-\u003e();\n```\n\n# NOTE\n\n## enforce LIST to simplify\n\n`Function::Return` makes the original function is called in list context whether the wrapped function is called in list, scalar, void context:\n\n```perl\nsub foo :Return(Str) { wantarray ? 'LIST!!' : 'NON!!' }\nmy $a = foo(); # =\u003e LIST!!\n```\n\nThe specified type checks against the value the original function was called in the list context.\n\n`wantarray` is convenient, but it sometimes causes confusion. So, in this module, we prioritize that it easy to understand the type of function return value.\n\n## requirements of type constraint\n\nThe requirements of type constraint of `Function::Return` is the same as for [Function::Parameters](https://metacpan.org/pod/Function%3A%3AParameters). Specific requirements are as follows:\n\n\\\u003e The only requirement is that the returned value (here referred to as $tc, for \"type constraint\") is an object that provides $tc-\u003echeck($value) and $tc-\u003eget\\_message($value) methods. check is called to determine whether a particular value is valid; it should return a true or false value. get\\_message is called on values that fail the check test; it should return a string that describes the error.\n\n## compare Return::Type\n\nBoth [Return::Type](https://metacpan.org/pod/Return%3A%3AType) and `Function::Return` perform type checking on function return value, but have some differences.\n\n1\\. `Function::Return` is not possible to specify different type constraints for scalar and list context, but `Return::Type` is possible.\n\n2\\. `Function::Return` check type constraint for void context, but `Return::Type` doesn't.\n\n3\\. `Function::Return` can be used together with `Function::Parameters::Info`, but `Return::Type` seems a bit difficult.\n\n# SEE ALSO\n\n[Sub::Meta](https://metacpan.org/pod/Sub%3A%3AMeta)\n\n# LICENSE\n\nCopyright (C) kfly8.\n\nThis library is free software; you can redistribute it and/or modify\nit under the same terms as Perl itself.\n\n# AUTHOR\n\nkfly8 \u003ckfly@cpan.org\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Fp5-function-return","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkfly8%2Fp5-function-return","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Fp5-function-return/lists"}