line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MojoX::GlobalEvents; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: A module to handle events |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
85309
|
use strict; |
|
2
|
|
|
|
|
18
|
|
|
2
|
|
|
|
|
52
|
|
6
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
67
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
956
|
use File::Find::Rule; |
|
2
|
|
|
|
|
14783
|
|
|
2
|
|
|
|
|
14
|
|
9
|
2
|
|
|
2
|
|
120
|
use File::Spec; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
36
|
|
10
|
2
|
|
|
2
|
|
979
|
use Mojo::File; |
|
2
|
|
|
|
|
427675
|
|
|
2
|
|
|
|
|
116
|
|
11
|
2
|
|
|
2
|
|
21
|
use Mojo::Util qw(class_to_path); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
172
|
|
12
|
2
|
|
|
2
|
|
14
|
use Scalar::Util qw(blessed); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
124
|
|
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
12
|
use base 'Exporter'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1284
|
|
15
|
|
|
|
|
|
|
our @EXPORT = qw(on publish); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my %subscriber; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub init { |
22
|
2
|
|
|
2
|
1
|
744
|
my ($class, $namespace) = @_; |
23
|
|
|
|
|
|
|
|
24
|
2
|
100
|
|
|
|
17
|
die 'ERROR: Missing namespace to setup global event.' |
25
|
|
|
|
|
|
|
unless defined $namespace; |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
|
|
5
|
my @spaces = split /::/, $namespace; |
28
|
1
|
|
|
|
|
4
|
my @dirs = map{ File::Spec->catdir( $_, @spaces ) }@INC; |
|
10
|
|
|
|
|
56
|
|
29
|
1
|
|
|
|
|
35
|
my @files = File::Find::Rule->file->name( '*.pm' )->in( @dirs ); |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
3376
|
for my $file ( @files ) { |
32
|
2
|
|
|
|
|
13
|
my @parts = @{ Mojo::File->new( $file )->to_array }; |
|
2
|
|
|
|
|
15
|
|
33
|
2
|
|
|
|
|
112
|
my $module = join '::', @parts; |
34
|
2
|
|
|
|
|
47
|
$module =~ s{.*?\Q$namespace\E}{$namespace}; |
35
|
2
|
|
|
|
|
11
|
$module =~ s{\.pm\z}{}; |
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
|
|
12
|
my $path = class_to_path $module; |
38
|
2
|
|
|
|
|
709
|
require $path; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub on { |
43
|
11
|
|
|
11
|
1
|
15680
|
my $object = shift; |
44
|
|
|
|
|
|
|
|
45
|
11
|
|
|
|
|
33
|
my ($event,$sub) = @_; |
46
|
|
|
|
|
|
|
|
47
|
11
|
100
|
|
|
|
50
|
if ( !blessed $object ) { |
48
|
9
|
|
|
|
|
15
|
$sub = $event; |
49
|
9
|
|
|
|
|
13
|
$event = $object; |
50
|
9
|
|
|
|
|
17
|
$object = ''; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
11
|
100
|
100
|
|
|
82
|
return if ref $event or !ref $sub or ref $sub ne 'CODE'; |
|
|
|
100
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
6
|
|
|
|
|
19
|
my $package = "$object"; |
56
|
6
|
100
|
|
|
|
26
|
$package = caller if !$package; |
57
|
6
|
|
|
|
|
82
|
$subscriber{$event}->{$package} = $sub; |
58
|
|
|
|
|
|
|
|
59
|
6
|
|
|
|
|
18
|
return 1; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub publish { |
63
|
8
|
|
|
8
|
1
|
6642
|
my ($event, @param) = @_; |
64
|
|
|
|
|
|
|
|
65
|
8
|
100
|
|
|
|
30
|
for my $package ( sort keys %{ $subscriber{$event} || {} } ) { |
|
8
|
|
|
|
|
65
|
|
66
|
10
|
|
|
|
|
82
|
$subscriber{$event}->{$package}->(@param); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
8
|
|
|
|
|
124
|
return 1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |