perl - Error handling with next in Try::Tiny catch block -


the following code (a simplified example, i'm iterating on list of objects , trying trap exception) performs error handling going next item in list. works, gives warning using loop control statement within catch subroutine:

use strict; use warnings; use try::tiny; use 5.010;  num: $num (1 .. 10) {    try {      if ($num == 7) {        die 'ugly number';      }    } catch {      chomp;     qq/got "$_"/;      next num;    };    qq/number $num/;  } 

outputs:

number 1 number 2 number 3 number 4 number 5 number 6 got "ugly number @ testtry.pl line 9." exiting subroutine via next @ testtry.pl line 14. exiting subroutine via next @ testtry.pl line 14. number 8 number 9 number 10 

i can see 2 ways work around -- shut warning in scope of usage scoped no warnings block, or copy error message off temporary variable , check it/next outside of catch block. former may have issues i'm overlooking, , second spreads out error handling bit. preferred, or there way i'm overlooking?

inside catch block, put no warnings 'exiting'. disable warning. strict , warnings pragmas there you, feel free disable them lexically when in way.

the perldiag page lists builtin warning , error categories. can see messages silenced disabling category , decide if worth it.

edit:

you can exploit successful try returns undef, on error value of catch block. allows do:

num: $num (1 .. 10) {    try {     die 'ugly number' if $num == 7;   } catch {      chomp;     qq/got "$_"/;      return 1;       # return true value   } , next num;   # go next iteration here, outside try/catch   qq/number $num/;  } 

however, find no warnings solution more elegant, , far more obvious.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -