{
    "mode": "perldoc",
    "parameter": "autodie::hints",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/autodie%3A%3Ahints/json",
    "generated": "2026-06-03T05:26:32Z",
    "synopsis": "package Your::Module;\nour %DOES = ( 'autodie::hints::provider' => 1 );\nsub AUTODIEHINTS {\nreturn {\nfoo => { scalar => HINTS, list => SOMEHINTS },\nbar => { scalar => HINTS, list => MOREHINTS },\n}\n}\n# Later, in your main program...\nuse Your::Module qw(foo bar);\nuse autodie      qw(:default foo bar);\nfoo();         # succeeds or dies based on scalar hints\n# Alternatively, hints can be set on subroutines we've\n# imported.\nuse autodie::hints;\nuse Some::Module qw(thinkpositive);\nBEGIN {\nautodie::hints->sethintsfor(\n\\&thinkpositive,\n{\nfail => sub { $[0] <= 0 }\n}\n)\n}\nuse autodie qw(thinkpositive);\nthinkpositive(...);    # Returns positive or dies.",
    "sections": {
        "NAME": {
            "content": "autodie::hints - Provide hints about user subroutines to autodie\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package Your::Module;\n\nour %DOES = ( 'autodie::hints::provider' => 1 );\n\nsub AUTODIEHINTS {\nreturn {\nfoo => { scalar => HINTS, list => SOMEHINTS },\nbar => { scalar => HINTS, list => MOREHINTS },\n}\n}\n\n# Later, in your main program...\n\nuse Your::Module qw(foo bar);\nuse autodie      qw(:default foo bar);\n\nfoo();         # succeeds or dies based on scalar hints\n\n# Alternatively, hints can be set on subroutines we've\n# imported.\n\nuse autodie::hints;\nuse Some::Module qw(thinkpositive);\n\nBEGIN {\nautodie::hints->sethintsfor(\n\\&thinkpositive,\n{\nfail => sub { $[0] <= 0 }\n}\n)\n}\nuse autodie qw(thinkpositive);\n\nthinkpositive(...);    # Returns positive or dies.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "",
            "subsections": [
                {
                    "name": "Introduction",
                    "content": "The autodie pragma is very smart when it comes to working with Perl's built-in functions. The\nbehaviour for these functions are fixed, and \"autodie\" knows exactly how they try to signal\nfailure.\n\nBut what about user-defined subroutines from modules? If you use \"autodie\" on a user-defined\nsubroutine then it assumes the following behaviour to demonstrate failure:\n\n*   A false value, in scalar context\n\n*   An empty list, in list context\n\n*   A list containing a single undef, in list context\n\nAll other return values (including the list of the single zero, and the list containing a single\nempty string) are considered successful. However, real-world code isn't always that easy.\nPerhaps the code you're working with returns a string containing the word \"FAIL\" upon failure,\nor a two element list containing \"(undef, \"human error message\")\". To make autodie work with\nthese sorts of subroutines, we have the *hinting interface*.\n\nThe hinting interface allows *hints* to be provided to \"autodie\" on how it should detect failure\nfrom user-defined subroutines. While these *can* be provided by the end-user of \"autodie\", they\nare ideally written into the module itself, or into a helper module or sub-class of \"autodie\"\nitself.\n\nWhat are hints?\nA *hint* is a subroutine or value that is checked against the return value of an autodying\nsubroutine. If the match returns true, \"autodie\" considers the subroutine to have failed.\n\nIf the hint provided is a subroutine, then \"autodie\" will pass the complete return value to that\nsubroutine. If the hint is any other value, then \"autodie\" will smart-match against the value\nprovided. In Perl 5.8.x there is no smart-match operator, and as such only subroutine hints are\nsupported in these versions.\n\nHints can be provided for both scalar and list contexts. Note that an autodying subroutine will\nnever see a void context, as \"autodie\" always needs to capture the return value for examination.\nAutodying subroutines called in void context act as if they're called in a scalar context, but\ntheir return value is discarded after it has been checked.\n"
                },
                {
                    "name": "Example hints",
                    "content": "Hints may consist of subroutine references, objects overloading smart-match, regular\nexpressions, and depending on Perl version possibly other things. You can specify different\nhints for how failure should be identified in scalar and list contexts.\n\nThese examples apply for use in the \"AUTODIEHINTS\" subroutine and when calling\n\"autodie::hints->sethintsfor()\".\n\nThe most common context-specific hints are:\n\n# Scalar failures always return undef:\n{  scalar => sub { !defined($[0]) }  }\n\n# Scalar failures return any false value [default expectation]:\n{  scalar => sub { ! $[0] }  }\n\n# Scalar failures always return zero explicitly:\n{  scalar => sub { defined($[0]) && $[0] eq '0' }  }\n\n# List failures always return an empty list:\n{  list => sub { !@ }  }\n\n# List failures return () or (undef) [default expectation]:\n{  list => sub { ! @ || @ == 1 && !defined $[0] }  }\n\n# List failures return () or a single false value:\n{  list => sub { ! @ || @ == 1 && !$[0] }  }\n\n# List failures return (undef, \"some string\")\n{  list => sub { @ == 2 && !defined $[0] }  }\n\n# Unsuccessful foo() returns 'FAIL' or 'FAIL' in scalar context,\n#                    returns (-1) in list context...\nautodie::hints->sethintsfor(\n\\&foo,\n{\nscalar => qr/^ ? FAIL $/xms,\nlist   => sub { @ == 1 && $[0] eq -1 },\n}\n);\n\n# Unsuccessful foo() returns 0 in all contexts...\nautodie::hints->sethintsfor(\n\\&foo,\n{\nscalar => sub { defined($[0]) && $[0] == 0 },\nlist   => sub { @ == 1 && defined($[0]) && $[0] == 0 },\n}\n);\n\nThis \"in all contexts\" construction is very common, and can be abbreviated, using the 'fail'\nkey. This sets both the \"scalar\" and \"list\" hints to the same value:\n\n# Unsuccessful foo() returns 0 in all contexts...\nautodie::hints->sethintsfor(\n\\&foo,\n{\nfail => sub { @ == 1 and defined $[0] and $[0] == 0 }\n}\n);\n\n# Unsuccessful thinkpositive() returns negative number on failure...\nautodie::hints->sethintsfor(\n\\&thinkpositive,\n{\nfail => sub { $[0] < 0 }\n}\n);\n\n# Unsuccessful mysystem() returns non-zero on failure...\nautodie::hints->sethintsfor(\n\\&mysystem,\n{\nfail => sub { $[0] != 0 }\n}\n);\n"
                }
            ]
        },
        "Manually setting hints from within your program": {
            "content": "If you are using a module which returns something special on failure, then you can manually\ncreate hints for each of the desired subroutines. Once the hints are specified, they are\navailable for all files and modules loaded thereafter, thus you can move this work into a module\nand it will still work.\n\nuse Some::Module qw(foo bar);\nuse autodie::hints;\n\nautodie::hints->sethintsfor(\n\\&foo,\n{\nscalar => SCALARHINT,\nlist   => LISTHINT,\n}\n);\nautodie::hints->sethintsfor(\n\\&bar,\n{ fail => SOMEHINT, }\n);\n\nIt is possible to pass either a subroutine reference (recommended) or a fully qualified\nsubroutine name as the first argument. This means you can set hints on modules that *might* get\nloaded:\n\nuse autodie::hints;\nautodie::hints->sethintsfor(\n'Some::Module:bar', { fail => SCALARHINT, }\n);\n\nThis technique is most useful when you have a project that uses a lot of third-party modules.\nYou can define all your possible hints in one-place. This can even be in a sub-class of autodie.\nFor example:\n\npackage my::autodie;\n\nuse parent qw(autodie);\nuse autodie::hints;\n\nautodie::hints->sethintsfor(...);\n\n1;\n\nYou can now \"use my::autodie\", which will work just like the standard \"autodie\", but is now\naware of any hints that you've set.\n",
            "subsections": []
        },
        "Adding hints to your module": {
            "content": "\"autodie\" provides a passive interface to allow you to declare hints for your module. These\nhints will be found and used by \"autodie\" if it is loaded, but otherwise have no effect (or\ndependencies) without autodie. To set these, your module needs to declare that it *does* the\n\"autodie::hints::provider\" role. This can be done by writing your own \"DOES\" method, using a\nsystem such as \"Class::DOES\" to handle the heavy-lifting for you, or declaring a %DOES package\nvariable with a \"autodie::hints::provider\" key and a corresponding true value.\n\nNote that checking for a %DOES hash is an \"autodie\"-only short-cut. Other modules do not use\nthis mechanism for checking roles, although you can use the \"Class::DOES\" module from the CPAN\nto allow it.\n\nIn addition, you must define a \"AUTODIEHINTS\" subroutine that returns a hash-reference\ncontaining the hints for your subroutines:\n\npackage Your::Module;\n\n# We can use the Class::DOES from the CPAN to declare adherence\n# to a role.\n\nuse Class::DOES 'autodie::hints::provider' => 1;\n\n# Alternatively, we can declare the role in %DOES.  Note that\n# this is an autodie specific optimisation, although Class::DOES\n# can be used to promote this to a true role declaration.\n\nour %DOES = ( 'autodie::hints::provider' => 1 );\n\n# Finally, we must define the hints themselves.\n\nsub AUTODIEHINTS {\nreturn {\nfoo => { scalar => HINTS, list => SOMEHINTS },\nbar => { scalar => HINTS, list => MOREHINTS },\nbaz => { fail => HINTS },\n}\n}\n\nThis allows your code to set hints without relying on \"autodie\" and \"autodie::hints\" being\nloaded, or even installed. In this way your code can do the right thing when \"autodie\" is\ninstalled, but does not need to depend upon it to function.\n",
            "subsections": []
        },
        "Insisting on hints": {
            "content": "When a user-defined subroutine is wrapped by \"autodie\", it will use hints if they are available,\nand otherwise reverts to the *default behaviour* described in the introduction of this document.\nThis can be problematic if we expect a hint to exist, but (for whatever reason) it has not been\nloaded.\n\nWe can ask autodie to *insist* that a hint be used by prefixing an exclamation mark to the start\nof the subroutine name. A lone exclamation mark indicates that *all* subroutines after it must\nhave hints declared.\n\n# foo() and bar() must have their hints defined\nuse autodie qw( !foo !bar baz );\n\n# Everything must have hints (recommended).\nuse autodie qw( ! foo bar baz );\n\n# bar() and baz() must have their hints defined\nuse autodie qw( foo ! bar baz );\n\n# Enable autodie for all of Perl's supported built-ins,\n# as well as for foo(), bar() and baz().  Everything must\n# have hints.\nuse autodie qw( ! :all foo bar baz );\n\nIf hints are not available for the specified subroutines, this will cause a compile-time error.\nInsisting on hints for Perl's built-in functions (eg, \"open\" and \"close\") is always successful.\n\nInsisting on hints is *strongly* recommended.\n",
            "subsections": []
        },
        "Diagnostics": {
            "content": "Attempts to sethintsfor unidentifiable subroutine\nYou've called \"autodie::hints->sethintsfor()\" using a subroutine reference, but that\nreference could not be resolved back to a subroutine name. It may be an anonymous subroutine\n(which can't be made autodying), or may lack a name for other reasons.\n\nIf you receive this error with a subroutine that has a real name, then you may have found a\nbug in autodie. See \"BUGS\" in autodie for how to report this.\n\nfail hints cannot be provided with either scalar or list hints for %s\nWhen defining hints, you can either supply both \"list\" and \"scalar\" keywords, *or* you can\nprovide a single \"fail\" keyword. You can't mix and match them.\n\n%s hint missing for %s\nYou've provided either a \"scalar\" hint without supplying a \"list\" hint, or vice-versa. You\n*must* supply both \"scalar\" and \"list\" hints, *or* a single \"fail\" hint.\n",
            "subsections": []
        },
        "ACKNOWLEDGEMENTS": {
            "content": "*   Dr Damian Conway for suggesting the hinting interface and providing the example usage.\n\n*   Jacinta Richardson for translating much of my ideas into this documentation.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright 2009, Paul Fenwick <pjf@perltraining.com.au>\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This module is free software. You may distribute it under the same terms as Perl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "autodie, Class::DOES\n",
            "subsections": []
        }
    },
    "summary": "autodie::hints - Provide hints about user subroutines to autodie",
    "flags": [],
    "examples": [],
    "see_also": []
}