From: Jesse Luehrs Date: Fri, 5 Jul 2013 18:51:07 +0000 (-0400) Subject: also die on catch/finally in scalar context (RT#81070) X-Git-Tag: Try-Tiny-0.14~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d2ae14adb7ae67c94c4e2cbc5d55bbef07c741a3;p=p5sagit%2FTry-Tiny.git also die on catch/finally in scalar context (RT#81070) --- diff --git a/Changes b/Changes index 9cd380c..607b31a 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for Try-Tiny {{$NEXT}} + - also throw an exception for catch/finally in scalar context (RT#81070) 0.13 2013-07-04 - fix tests failing on 5.6.x due to differing DESTROY semantics diff --git a/lib/Try/Tiny.pm b/lib/Try/Tiny.pm index 6d23287..f291b14 100644 --- a/lib/Try/Tiny.pm +++ b/lib/Try/Tiny.pm @@ -104,7 +104,7 @@ sub try (&;@) { sub catch (&;@) { my ( $block, @rest ) = @_; - croak 'Useless bare catch()' unless defined wantarray; + croak 'Useless bare catch()' unless wantarray; return ( bless(\$block, 'Try::Tiny::Catch'), @@ -115,7 +115,7 @@ sub catch (&;@) { sub finally (&;@) { my ( $block, @rest ) = @_; - croak 'Useless bare finally()' unless defined wantarray; + croak 'Useless bare finally()' unless wantarray; return ( bless(\$block, 'Try::Tiny::Finally'), diff --git a/t/erroneous_usage.t b/t/erroneous_usage.t index 789cd68..c8cc478 100644 --- a/t/erroneous_usage.t +++ b/t/erroneous_usage.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 6; +use Test::More tests => 8; use Try::Tiny; @@ -53,3 +53,25 @@ throws_ok { do { 2 } } qr/\Qtry() encountered an unexpected argument (2) - perhaps a missing semi-colon before or at/, 'Unterminated try detected'; + +sub foo { + try { 0 }; catch { 2 } +} + +throws_ok { + if (foo()) { + # ... + } +} qr/\QUseless bare catch/, + 'Bare catch at the end of a function call'; + +sub bar { + try { 0 }; finally { 2 } +} + +throws_ok { + if (bar()) { + # ... + } +} qr/\QUseless bare finally/, + 'Bare finally at the end of a function call';