| 1 | package App::Environ; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.008000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | our $VERSION = '0.22'; | |||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 4 | my %REGD_HANDLERS; | |||||||||
| 12 | my %MODULES_IDX; | ||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 7 | ▶ | sub register { | ||||||||
|
Subroutine
register: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 16 | 7 | my $class = shift; | |||||||||
| 17 | 7 | my $module_class = shift; | |||||||||
| 18 | 7 | my %handlers = @_; | |||||||||
| 19 | |||||||||||
| 20 | 7 | ▶ | unless ( defined $module_class ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 1 | croak 'Module class must be specified'; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 6 | ▶ | unless ( exists $MODULES_IDX{$module_class} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 6 | $MODULES_IDX{$module_class} = {}; | |||||||||
| 26 | } | ||||||||||
| 27 | 6 | my $events_idx = $MODULES_IDX{$module_class}; | |||||||||
| 28 | |||||||||||
| 29 | 6 | ▶ | while ( my ( $event_name, $handler ) = each %handlers ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 20 | ▶ | next if exists $events_idx->{$event_name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 20 | $events_idx->{$event_name} = 1; | |||||||||
| 33 | 20 | ▶ | unless ( exists $REGD_HANDLERS{$event_name} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 13 | $REGD_HANDLERS{$event_name} = []; | |||||||||
| 35 | } | ||||||||||
| 36 | 20 | ▶ | if ( $event_name =~ m/\:r$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 8 | unshift( @{ $REGD_HANDLERS{$event_name} }, $handler ); | |||||||||
| 38 | } | ||||||||||
| 39 | else { | ||||||||||
| 40 | 12 | push( @{ $REGD_HANDLERS{$event_name} }, $handler ); | |||||||||
| 41 | } | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 6 | return; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 8 | ▶ | sub send_event { | ||||||||
|
Subroutine
send_event: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 48 | 8 | my $class = shift; | |||||||||
| 49 | 8 | my $event_name = shift; | |||||||||
| 50 | 8 | ▶ | my $cb = pop if ref( $_[-1] ) eq 'CODE'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 8 | my @args = @_; | |||||||||
| 52 | |||||||||||
| 53 | 8 | ▶ | unless ( defined $event_name ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 1 | croak 'Event name must be specified'; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 7 | ▶ | unless ( exists $REGD_HANDLERS{$event_name} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 1 | ▶ | $cb->() if defined $cb; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 1 | return; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 6 | my @handlers = @{ $REGD_HANDLERS{$event_name} }; | |||||||||
| 63 | 6 | ▶ | if ( defined $cb ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 2 | $class->_process_async( \@handlers, \@args, $cb ); | |||||||||
| 65 | } | ||||||||||
| 66 | else { | ||||||||||
| 67 | 4 | foreach my $handler (@handlers) { | |||||||||
| 68 | 11 | $handler->(@args); | |||||||||
| 69 | } | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 5 | return; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 4 | ▶ | sub _process_async { | ||||||||
|
Subroutine
_process_async: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 4 | my $class = shift; | |||||||||
| 77 | 4 | my $handlers = shift; | |||||||||
| 78 | 4 | my $args = shift; | |||||||||
| 79 | 4 | my $cb = shift; | |||||||||
| 80 | |||||||||||
| 81 | 4 | my $handler = shift @{$handlers}; | |||||||||
| 82 | |||||||||||
| 83 | 4 | $handler->( @{$args}, | |||||||||
| 84 | 4 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 85 | 4 | my $err = shift; | |||||||||
| 86 | |||||||||||
| 87 | 4 | ▶ | if ( defined $err ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 1 | $cb->($err); | |||||||||
| 89 | 1 | return; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 3 | ▶ | if ( @{$handlers} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 2 | $class->_process_async( $handlers, $args, $cb ); | |||||||||
| 94 | 2 | return; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 1 | $cb->(); | |||||||||
| 98 | } | ||||||||||
| 99 | 4 | ); | |||||||||
| 100 | |||||||||||
| 101 | 4 | return; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 4 | 1; | |||||||||
| 105 | __END__ | ||||||||||