From: Jesse Luehrs Date: Tue, 10 May 2011 00:35:48 +0000 (-0500) Subject: better example for returning from a try block (forwardever) X-Git-Tag: Try-Tiny-0.11~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6651956b20f3018a9faed6de7634870dbce07721;p=p5sagit%2FTry-Tiny.git better example for returning from a try block (forwardever) --- diff --git a/lib/Try/Tiny.pm b/lib/Try/Tiny.pm index 382bfdb..5ab8160 100644 --- a/lib/Try/Tiny.pm +++ b/lib/Try/Tiny.pm @@ -455,12 +455,32 @@ or C returns from the C block, not from the parent sub (note that this is also how C works, but not how L works): - sub bar { - try { return "foo" }; - return "baz"; - } + sub parent_sub { + try { + die; + } + catch { + return; + }; + + say "this text WILL be displayed, even though an exception is thrown"; + } + +Instead, you should capture the return value: + + sub parent_sub { + my $success = try { + die; + 1; + } + return unless $success; + + say "This text WILL NEVER appear!"; + } - say bar(); # "baz" +Note that if you have a catch block, it must return undef for this to work, +since if a catch block exists, its return value is returned in place of undef +when an exception is thrown. =item *