File Coverage

blib/lib/Math/ModInt/Event.pm
Criterion Covered Total %
statement 63 63 100.0
branch n/a
condition 2 2 100.0
subroutine 22 22 100.0
pod 4 4 100.0
total 91 91 100.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2010-2019 Martin Becker, Blaubeuren.
2             # This package is free software; you can distribute it and/or modify it
3             # under the terms of the Artistic License 2.0 (see LICENSE file).
4              
5             package Math::ModInt::Event;
6              
7 11     11   155 use 5.006;
  11         34  
8 11     11   55 use strict;
  11         19  
  11         256  
9 11     11   58 use warnings;
  11         19  
  11         326  
10 11     11   59 use Carp qw(croak);
  11         15  
  11         591  
11 11     11   4557 use Math::ModInt::Event::Trap;
  11         21  
  11         820  
12              
13             # ----- class data -----
14              
15             BEGIN {
16 11     11   62 require Exporter;
17 11         170 our @ISA = qw(Exporter);
18 11         49 our @EXPORT_OK = qw(
19             AnyEvent Unrecoverable Recoverable UsageError Nonexistent
20             LoadingFailure UndefinedResult DifferentModuli
21             );
22 11         20 our @CARP_NOT = qw(Math::ModInt);
23 11         261 our $VERSION = '0.012';
24             }
25              
26             # Math::ModInt::Event=ARRAY(...)
27              
28             # ............ index ............ # ............ value ............
29 11     11   80 use constant F_DESCRIPTION => 0; # a short event description string
  11         21  
  11         663  
30 11     11   65 use constant NFIELDS => 1;
  11         17  
  11         1779  
31              
32             # ----- private subroutines -----
33              
34             # create a new event subclass and return a singleton instance of that class
35             # - description string is made of the words in the camel-case class name
36             # - parent class is taken from optional parent object or defaults to Event.
37             sub _make {
38 88     88   181 my ($class, $parent) = @_;
39 88         602 my $description = lc join q[ ], split /(?<=[a-z])(?=[A-Z])/, $class;
40 88         204 $class = join '::', __PACKAGE__, $class;
41 88   100     5735 eval join '', '@', $class, '::ISA = qw(', ref $parent || __PACKAGE__, ')';
42 88         6443 return bless [$description], $class;
43             }
44              
45             # ----- public methods -----
46              
47             # singleton event constructors:
48              
49 11     11   69 use constant AnyEvent => _make('AnyEvent');
  11         18  
  11         27  
50 11     11   77 use constant Unrecoverable => _make('Unrecoverable', AnyEvent);
  11         21  
  11         36  
51 11     11   64 use constant UsageError => _make('UsageError', Unrecoverable);
  11         23  
  11         25  
52 11     11   62 use constant Nonexistent => _make('Nonexistent', Unrecoverable);
  11         19  
  11         25  
53 11     11   66 use constant LoadingFailure => _make('LoadingFailure', Unrecoverable);
  11         24  
  11         24  
54 11     11   63 use constant Recoverable => _make('Recoverable', AnyEvent);
  11         28  
  11         34  
55 11     11   65 use constant UndefinedResult => _make('UndefinedResult', Recoverable);
  11         19  
  11         25  
56 11     11   65 use constant DifferentModuli => _make('DifferentModuli', Recoverable);
  11         39  
  11         28  
57              
58             sub description {
59 39     39 1 56 my ($this) = @_;
60 39         4063 return $this->[F_DESCRIPTION];
61             }
62              
63 36     36 1 89 sub is_recoverable { 0 }
64 74     74   148 sub Math::ModInt::Event::Recoverable::is_recoverable { 1 }
65              
66             sub trap {
67 15     15 1 443 my ($this, $handler) = @_;
68 15         37 return Math::ModInt::Event::Trap->new($this, $handler);
69             }
70              
71             sub raise {
72 114     114 1 200 my ($this, @details) = @_;
73 114         340 Math::ModInt::Event::Trap->broadcast($this, @details);
74 74         117 return $this;
75             }
76              
77             1;
78              
79             __END__