File Coverage

blib/lib/Syntax/Feature/Try.pm
Criterion Covered Total %
statement 52 52 100.0
branch 20 20 100.0
condition 3 3 100.0
subroutine 14 14 100.0
pod 0 2 0.0
total 89 91 97.8


line stmt bran cond sub pod time code
1             package Syntax::Feature::Try;
2              
3 25     25   3570335 use 5.014;
  25         106  
  25         916  
4 25     25   141 use strict;
  25         50  
  25         933  
5 25     25   140 use warnings;
  25         48  
  25         718  
6 25     25   132 use XSLoader;
  25         55  
  25         643  
7 25     25   153 use Scalar::Util qw/ blessed /;
  25         72  
  25         5550  
8              
9             BEGIN {
10 25     25   68 our $VERSION = '0.009';
11 25         33954 XSLoader::load();
12             }
13              
14             sub install {
15 55     55 0 272870 $^H{+HINTKEY_ENABLED} = 1;
16             }
17              
18             sub uninstall {
19 1     1 0 159 $^H{+HINTKEY_ENABLED} = 0;
20             }
21              
22             # TODO convert "our" to "my" variables
23             our $return_values;
24              
25             sub _statement {
26 246     246   749200 my ($try_block, $catch_list, $finally_block) = @_;
27              
28 246         23164 my $stm_handler = bless {finally => $finally_block}, __PACKAGE__;
29              
30 246         20652 local $@;
31 246         21915 my $exception = run_block($stm_handler, $try_block, 1);
32 245 100 100     6825040 if ($exception and $catch_list) {
33 121         78254 my $catch_block = _get_exception_handler($exception, $catch_list);
34 121 100       14526 if ($catch_block) {
35 120         16636 $exception = run_block($stm_handler, $catch_block, 1, $exception);
36             }
37             }
38              
39 245 100       141901 if ($finally_block) {
40 164         23149 delete $stm_handler->{finally};
41 164         29747 run_block($stm_handler, $finally_block);
42             }
43              
44 243 100       84650 if ($exception) {
45 12         65 _rethrow($exception);
46             }
47              
48 231         20664 $return_values = $stm_handler->{return};
49 231         20696 return $stm_handler->{return};
50             }
51              
52             sub DESTROY {
53 246     246   29056 my ($self) = @_;
54 246 100       70082 run_block($self, $self->{finally}) if $self->{finally};
55             }
56              
57             sub _get_exception_handler {
58 121     121   14328 my ($exception, $catch_list) = @_;
59              
60 121         14249 foreach my $item (@{ $catch_list }) {
  121         28813  
61 165         27137 my ($block_ref, @args) = @$item;
62 165 100       33888 return $block_ref if _exception_match_args($exception, @args);
63             }
64             }
65              
66             sub _exception_match_args {
67 165     165   26792 my ($exception, $className) = @_;
68              
69 165 100       27165 if (defined $className) {
70 145 100       28136 return 0 if not blessed($exception);
71 138 100       56086 return 0 if not $exception->isa($className);
72             }
73 120         43436 return 1; # without args catch all exceptions
74             }
75              
76             sub _rethrow {
77 12     12   22 my ($exception) = @_;
78 12         61 local $SIG{__DIE__} = undef;
79 12         81 die $exception;
80             }
81              
82             sub _get_return_value {
83 103     103   8686 my $return = $return_values;
84 103         8068 undef $return_values;
85              
86 103 100       32362 return wantarray ? @$return : $return->[0];
87             }
88              
89             1;
90              
91             __END__