From: anaxagoras Date: Tue, 21 Jan 2014 16:47:24 +0000 (-0500) Subject: Better docs on capturing success with catch block X-Git-Tag: Try-Tiny-0.19~5^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c87ad8ce69beca36045baf185b4c9f48914cbae8;p=p5sagit%2FTry-Tiny.git Better docs on capturing success with catch block While there is a note that calls it out below, I believe the behavior and usage of capturing success when also using a catch{} block is sufficiently different to the example that it merits its own example in the same code block. My rationale is twofold: 1) Because it is sufficiently different in behavior, giving an example goes a long way towards understanding. 2) Because this is a critical feature to have fail and it might be easy for someone reading the docs to digest the example and move on before reading the note below (which is unemphasized), it is best to have the second code example alongside the first with a meaningful name so that they are both digested at the same time. --- diff --git a/lib/Try/Tiny.pm b/lib/Try/Tiny.pm index f97e9f7..588451c 100644 --- a/lib/Try/Tiny.pm +++ b/lib/Try/Tiny.pm @@ -500,6 +500,19 @@ Instead, you should capture the return value: say "This text WILL NEVER appear!"; } + + # or + + sub parent_sub_with_catch { + my $success = try { + die; + 1; + } + catch { + # do something with $_ + return undef; #see note + }; + } Note that if you have a C block, it must return C for this to work, since if a C block exists, its return value is returned in place of C