{
    "content": [
        {
            "type": "text",
            "text": "# Try::Tiny (perldoc)\n\n## NAME\n\nTry::Tiny - Minimal try/catch with proper preservation of $@\n\n## SYNOPSIS\n\nYou can use Try::Tiny's \"try\" and \"catch\" to expect and handle exceptional conditions, avoiding\nquirks in Perl and common mistakes:\n# handle errors with a catch handler\ntry {\ndie \"foo\";\n} catch {\nwarn \"caught error: $\"; # not $@\n};\nYou can also use it like a standalone \"eval\" to catch and ignore any error conditions.\nObviously, this is an extreme measure not to be undertaken lightly:\n# just silence errors\ntry {\ndie \"foo\";\n};\n\n## DESCRIPTION\n\nThis module provides bare bones \"try\"/\"catch\"/\"finally\" statements that are designed to minimize\ncommon mistakes with eval blocks, and NOTHING else.\n\n## Sections\n\n- **NAME**\n- **VERSION**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **EXPORTS**\n- **BACKGROUND**\n- **ALTERNATE SYNTAX**\n- **CAVEATS**\n- **SEE ALSO**\n- **LIGHTNING TALK**\n- **SUPPORT**\n- **AUTHORS**\n- **CONTRIBUTORS**\n- **COPYRIGHT AND LICENCE**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Try::Tiny",
        "section": "",
        "mode": "perldoc",
        "summary": "Try::Tiny - Minimal try/catch with proper preservation of $@",
        "synopsis": "You can use Try::Tiny's \"try\" and \"catch\" to expect and handle exceptional conditions, avoiding\nquirks in Perl and common mistakes:\n# handle errors with a catch handler\ntry {\ndie \"foo\";\n} catch {\nwarn \"caught error: $\"; # not $@\n};\nYou can also use it like a standalone \"eval\" to catch and ignore any error conditions.\nObviously, this is an extreme measure not to be undertaken lightly:\n# just silence errors\ntry {\ndie \"foo\";\n};",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "VERSION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 18,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 42,
                "subsections": []
            },
            {
                "name": "EXPORTS",
                "lines": 90,
                "subsections": []
            },
            {
                "name": "BACKGROUND",
                "lines": 85,
                "subsections": []
            },
            {
                "name": "ALTERNATE SYNTAX",
                "lines": 15,
                "subsections": []
            },
            {
                "name": "CAVEATS",
                "lines": 110,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 26,
                "subsections": []
            },
            {
                "name": "LIGHTNING TALK",
                "lines": 9,
                "subsections": []
            },
            {
                "name": "SUPPORT",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "AUTHORS",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "CONTRIBUTORS",
                "lines": 48,
                "subsections": []
            },
            {
                "name": "COPYRIGHT AND LICENCE",
                "lines": 6,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "Try::Tiny - Minimal try/catch with proper preservation of $@\n",
                "subsections": []
            },
            "VERSION": {
                "content": "version 0.31\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "You can use Try::Tiny's \"try\" and \"catch\" to expect and handle exceptional conditions, avoiding\nquirks in Perl and common mistakes:\n\n# handle errors with a catch handler\ntry {\ndie \"foo\";\n} catch {\nwarn \"caught error: $\"; # not $@\n};\n\nYou can also use it like a standalone \"eval\" to catch and ignore any error conditions.\nObviously, this is an extreme measure not to be undertaken lightly:\n\n# just silence errors\ntry {\ndie \"foo\";\n};\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This module provides bare bones \"try\"/\"catch\"/\"finally\" statements that are designed to minimize\ncommon mistakes with eval blocks, and NOTHING else.\n\nThis is unlike TryCatch which provides a nice syntax and avoids adding another call stack layer,\nand supports calling \"return\" from the \"try\" block to return from the parent subroutine. These\nextra features come at a cost of a few dependencies, namely Devel::Declare and Scope::Upper\nwhich are occasionally problematic, and the additional catch filtering uses Moose type\nconstraints which may not be desirable either.\n\nThe main focus of this module is to provide simple and reliable error handling for those having\na hard time installing TryCatch, but who still want to write correct \"eval\" blocks without 5\nlines of boilerplate each time.\n\nIt's designed to work as correctly as possible in light of the various pathological edge cases\n(see \"BACKGROUND\") and to be compatible with any style of error values (simple strings,\nreferences, objects, overloaded objects, etc).\n\nIf the \"try\" block dies, it returns the value of the last statement executed in the \"catch\"\nblock, if there is one. Otherwise, it returns \"undef\" in scalar context or the empty list in\nlist context. The following examples all assign \"bar\" to $x:\n\nmy $x = try { die \"foo\" } catch { \"bar\" };\nmy $x = try { die \"foo\" } || \"bar\";\nmy $x = (try { die \"foo\" }) // \"bar\";\n\nmy $x = eval { die \"foo\" } || \"bar\";\n\nYou can add \"finally\" blocks, yielding the following:\n\nmy $x;\ntry { die 'foo' } finally { $x = 'bar' };\ntry { die 'foo' } catch { warn \"Got a die: $\" } finally { $x = 'bar' };\n\n\"finally\" blocks are always executed making them suitable for cleanup code which cannot be\nhandled using local. You can add as many \"finally\" blocks to a given \"try\" block as you like.\n\nNote that adding a \"finally\" block without a preceding \"catch\" block suppresses any errors. This\nbehaviour is consistent with using a standalone \"eval\", but it is not consistent with\n\"try\"/\"finally\" patterns found in other programming languages, such as Java, Python, Javascript\nor C#. If you learned the \"try\"/\"finally\" pattern from one of these languages, watch out for\nthis.\n",
                "subsections": []
            },
            "EXPORTS": {
                "content": "All functions are exported by default using Exporter.\n\nIf you need to rename the \"try\", \"catch\" or \"finally\" keyword consider using Sub::Import to get\nSub::Exporter's flexibility.\n\ntry (&;@)\nTakes one mandatory \"try\" subroutine, an optional \"catch\" subroutine and \"finally\"\nsubroutine.\n\nThe mandatory subroutine is evaluated in the context of an \"eval\" block.\n\nIf no error occurred the value from the first block is returned, preserving list/scalar\ncontext.\n\nIf there was an error and the second subroutine was given it will be invoked with the error\nin $ (localized) and as that block's first and only argument.\n\n$@ does not contain the error. Inside the \"catch\" block it has the same value it had before\nthe \"try\" block was executed.\n\nNote that the error may be false, but if that happens the \"catch\" block will still be\ninvoked.\n\nOnce all execution is finished then the \"finally\" block, if given, will execute.\n\ncatch (&;@)\nIntended to be used in the second argument position of \"try\".\n\nReturns a reference to the subroutine it was given but blessed as \"Try::Tiny::Catch\" which\nallows try to decode correctly what to do with this code reference.\n\ncatch { ... }\n\nInside the \"catch\" block the caught error is stored in $, while previous value of $@ is\nstill available for use. This value may or may not be meaningful depending on what happened\nbefore the \"try\", but it might be a good idea to preserve it in an error stack.\n\nFor code that captures $@ when throwing new errors (i.e. Class::Throwable), you'll need to\ndo:\n\nlocal $@ = $;\n\nfinally (&;@)\ntry     { ... }\ncatch   { ... }\nfinally { ... };\n\nOr\n\ntry     { ... }\nfinally { ... };\n\nOr even\n\ntry     { ... }\nfinally { ... }\ncatch   { ... };\n\nIntended to be the second or third element of \"try\". \"finally\" blocks are always executed in\nthe event of a successful \"try\" or if \"catch\" is run. This allows you to locate cleanup code\nwhich cannot be done via \"local()\" e.g. closing a file handle.\n\nWhen invoked, the \"finally\" block is passed the error that was caught. If no error was\ncaught, it is passed nothing. (Note that the \"finally\" block does not localize $ with the\nerror, since unlike in a \"catch\" block, there is no way to know if \"$ == undef\" implies\nthat there were no errors.) In other words, the following code does just what you would\nexpect:\n\ntry {\ndiesometimes();\n} catch {\n# ...code run in case of error\n} finally {\nif (@) {\nprint \"The try block died with: @\\n\";\n} else {\nprint \"The try block ran without error.\\n\";\n}\n};\n\nYou must always do your own error handling in the \"finally\" block. \"Try::Tiny\" will not do\nanything about handling possible errors coming from code located in these blocks.\n\nFurthermore exceptions in \"finally\" blocks are not trappable and are unable to influence the\nexecution of your program. This is due to limitation of \"DESTROY\"-based scope guards, which\n\"finally\" is implemented on top of. This may change in a future version of Try::Tiny.\n\nIn the same way \"catch()\" blesses the code reference this subroutine does the same except it\nbless them as \"Try::Tiny::Finally\".\n",
                "subsections": []
            },
            "BACKGROUND": {
                "content": "There are a number of issues with \"eval\".\n\nClobbering $@\nWhen you run an \"eval\" block and it succeeds, $@ will be cleared, potentially clobbering an\nerror that is currently being caught.\n\nThis causes action at a distance, clearing previous errors your caller may have not yet handled.\n\n$@ must be properly localized before invoking \"eval\" in order to avoid this issue.\n\nMore specifically, before Perl version 5.14.0 $@ was clobbered at the beginning of the \"eval\",\nwhich also made it impossible to capture the previous error before you die (for instance when\nmaking exception objects with error stacks).\n\nFor this reason \"try\" will actually set $@ to its previous value (the one available before\nentering the \"try\" block) in the beginning of the \"eval\" block.\n\nLocalizing $@ silently masks errors\nInside an \"eval\" block, \"die\" behaves sort of like:\n\nsub die {\n$@ = $[0];\nreturnundeffromeval();\n}\n\nThis means that if you were polite and localized $@ you can't die in that scope, or your error\nwill be discarded (printing \"Something's wrong\" instead).\n\nThe workaround is very ugly:\n\nmy $error = do {\nlocal $@;\neval { ... };\n$@;\n};\n\n...\ndie $error;\n\n$@ might not be a true value\nThis code is wrong:\n\nif ( $@ ) {\n...\n}\n\nbecause due to the previous caveats it may have been unset.\n\n$@ could also be an overloaded error object that evaluates to false, but that's asking for\ntrouble anyway.\n\nThe classic failure mode (fixed in Perl 5.14.0) is:\n\nsub Object::DESTROY {\neval { ... }\n}\n\neval {\nmy $obj = Object->new;\n\ndie \"foo\";\n};\n\nif ( $@ ) {\n\n}\n\nIn this case since \"Object::DESTROY\" is not localizing $@ but still uses \"eval\", it will set $@\nto \"\".\n\nThe destructor is called when the stack is unwound, after \"die\" sets $@ to \"foo at Foo.pm line\n42\\n\", so by the time \"if ( $@ )\" is evaluated it has been cleared by \"eval\" in the destructor.\n\nThe workaround for this is even uglier than the previous ones. Even though we can't save the\nvalue of $@ from code that doesn't localize, we can at least be sure the \"eval\" was aborted due\nto an error:\n\nmy $failed = not eval {\n...\n\nreturn 1;\n};\n\nThis is because an \"eval\" that caught a \"die\" will always return a false value.\n",
                "subsections": []
            },
            "ALTERNATE SYNTAX": {
                "content": "Using Perl 5.10 you can use \"Switch statements\" in perlsyn (but please don't, because that\nsyntax has since been deprecated because there was too much unexpected magical behaviour).\n\nThe \"catch\" block is invoked in a topicalizer context (like a \"given\" block), but note that you\ncan't return a useful value from \"catch\" using the \"when\" blocks without an explicit \"return\".\n\nThis is somewhat similar to Perl 6's \"CATCH\" blocks. You can use it to concisely match errors:\n\ntry {\nrequire Foo;\n} catch {\nwhen (/^Can't locate .*?\\.pm in \\@INC/) { } # ignore\ndefault { die $ }\n};\n",
                "subsections": []
            },
            "CAVEATS": {
                "content": "*   @ is not available within the \"try\" block, so you need to copy your argument list. In case\nyou want to work with argument values directly via @ aliasing (i.e. allow \"$[1] = \"foo\"\"),\nyou need to pass @ by reference:\n\nsub foo {\nmy ( $self, @args ) = @;\ntry { $self->bar(@args) }\n}\n\nor\n\nsub barinplace {\nmy $self = shift;\nmy $args = \\@;\ntry { $ = $self->bar($) for @$args }\n}\n\n*   \"return\" returns from the \"try\" block, not from the parent sub (note that this is also how\n\"eval\" works, but not how TryCatch works):\n\nsub parentsub {\ntry {\ndie;\n}\ncatch {\nreturn;\n};\n\nsay \"this text WILL be displayed, even though an exception is thrown\";\n}\n\nInstead, you should capture the return value:\n\nsub parentsub {\nmy $success = try {\ndie;\n1;\n};\nreturn unless $success;\n\nsay \"This text WILL NEVER appear!\";\n}\n# OR\nsub parentsubwithcatch {\nmy $success = try {\ndie;\n1;\n}\ncatch {\n# do something with $\nreturn undef; #see note\n};\nreturn unless $success;\n\nsay \"This text WILL NEVER appear!\";\n}\n\nNote that if you have a \"catch\" block, it must return \"undef\" for this to work, since if a\n\"catch\" block exists, its return value is returned in place of \"undef\" when an exception is\nthrown.\n\n*   \"try\" introduces another caller stack frame. Sub::Uplevel is not used. Carp will not report\nthis when using full stack traces, though, because %Carp::Internal is used. This lack of\nmagic is considered a feature.\n\n*   The value of $ in the \"catch\" block is not guaranteed to be the value of the exception\nthrown ($@) in the \"try\" block. There is no safe way to ensure this, since \"eval\" may be\nused unhygienically in destructors. The only guarantee is that the \"catch\" will be called if\nan exception is thrown.\n\n*   The return value of the \"catch\" block is not ignored, so if testing the result of the\nexpression for truth on success, be sure to return a false value from the \"catch\" block:\n\nmy $obj = try {\nMightFail->new;\n} catch {\n...\n\nreturn; # avoid returning a true value;\n};\n\nreturn unless $obj;\n\n*   $SIG{DIE} is still in effect.\n\nThough it can be argued that $SIG{DIE} should be disabled inside of \"eval\" blocks, since\nit isn't people have grown to rely on it. Therefore in the interests of compatibility, \"try\"\ndoes not disable $SIG{DIE} for the scope of the error throwing code.\n\n*   Lexical $ may override the one set by \"catch\".\n\nFor example Perl 5.10's \"given\" form uses a lexical $, creating some confusing behavior:\n\ngiven ($foo) {\nwhen (...) {\ntry {\n...\n} catch {\nwarn $; # will print $foo, not the error\nwarn $[0]; # instead, get the error like this\n}\n}\n}\n\nNote that this behavior was changed once again in Perl5 version 18\n<https://metacpan.org/module/perldelta#given-now-aliases-the-global->. However, since the\nentirety of lexical $ is now considered experimental\n<https://metacpan.org/module/perldelta#Lexical--is-now-experimental>, it is unclear whether\nthe new version 18 behavior is final.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "Syntax::Keyword::Try\nOnly available on perls >= 5.14, with a slightly different syntax (e.g. no trailing \";\"\nbecause it's actually a keyword, not a sub, but this means you can \"return\" and \"next\"\nwithin it). Use Feature::Compat::Try to automatically switch to the native \"try\" syntax in\nnewer perls (when available). See also Try Catch Exception Handling.\n\nTryCatch\nMuch more feature complete, more convenient semantics, but at the cost of implementation\ncomplexity.\n\nautodie\nAutomatic error throwing for builtin functions and more. Also designed to work well with\n\"given\"/\"when\".\n\nThrowable\nA lightweight role for rolling your own exception classes.\n\nError\nException object implementation with a \"try\" statement. Does not localize $@.\n\nException::Class::TryCatch\nProvides a \"catch\" statement, but properly calling \"eval\" is your responsibility.\n\nThe \"try\" keyword pushes $@ onto an error stack, avoiding some of the issues with $@, but\nyou still need to localize to prevent clobbering.\n",
                "subsections": []
            },
            "LIGHTNING TALK": {
                "content": "I gave a lightning talk about this module, you can see the slides (Firefox only):\n\n<http://web.archive.org/web/20100628040134/http://nothingmuch.woobling.org/talks/takahashi.xul>\n\nOr read the source:\n\n<http://web.archive.org/web/20100305133605/http://nothingmuch.woobling.org/talks/yapcasia2009/\ntrytiny.yml>\n",
                "subsections": []
            },
            "SUPPORT": {
                "content": "Bugs may be submitted through the RT bug tracker\n<https://rt.cpan.org/Public/Dist/Display.html?Name=Try-Tiny> (or bug-Try-Tiny@rt.cpan.org\n<mailto:bug-Try-Tiny@rt.cpan.org>).\n",
                "subsections": []
            },
            "AUTHORS": {
                "content": "*   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>\n\n*   Jesse Luehrs <doy@tozt.net>\n",
                "subsections": []
            },
            "CONTRIBUTORS": {
                "content": "*   Karen Etheridge <ether@cpan.org>\n\n*   Peter Rabbitson <ribasushi@cpan.org>\n\n*   Ricardo Signes <rjbs@cpan.org>\n\n*   Mark Fowler <mark@twoshortplanks.com>\n\n*   Graham Knop <haarg@haarg.org>\n\n*   Aristotle Pagaltzis <pagaltzis@gmx.de>\n\n*   Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>\n\n*   Lukas Mai <l.mai@web.de>\n\n*   Alex <alex@koban.(none)>\n\n*   anaxagoras <walkeraj@gmail.com>\n\n*   Andrew Yates <ayates@haddock.local>\n\n*   awalker <awalker@sourcefire.com>\n\n*   chromatic <chromatic@wgz.org>\n\n*   cm-perl <cm-perl@users.noreply.github.com>\n\n*   David Lowe <davidl@lokku.com>\n\n*   Glenn Fowler <cebjyre@cpan.org>\n\n*   Hans Dieter Pearcey <hdp@weftsoar.net>\n\n*   Jens Berthold <jens@jebecs.de>\n\n*   Jonathan Yu <JAWNSY@cpan.org>\n\n*   Marc Mims <marc@questright.com>\n\n*   Mark Stosberg <mark@stosberg.com>\n\n*   Pali <pali@cpan.org>\n\n*   Paul Howarth <paul@city-fan.org>\n\n*   Rudolf Leermakers <rudolf@hatsuseno.org>\n",
                "subsections": []
            },
            "COPYRIGHT AND LICENCE": {
                "content": "This software is Copyright (c) 2009 by יובל קוג'מן (Yuval Kogman).\n\nThis is free software, licensed under:\n\nThe MIT (X11) License\n",
                "subsections": []
            }
        }
    }
}