« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Accessor.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Teng.pm »
| 1 | package Dwarf::Trigger; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent qw/Exporter/; | ||
| 4 | use Scalar::Util (); | ||
| 5 | |||
| 6 | our @EXPORT = qw/add_trigger call_trigger get_trigger_code/; | ||
| 7 | |||
| 8 | sub add_trigger { | ||
| 9 | my ($class, %args) = @_; | ||
| 10 | if (ref $class) { | ||
| 11 | while (my ($hook, $code) = each %args) { | ||
| 12 | next unless ref $code eq 'CODE'; | ||
| 13 | push @{$class->{_trigger}->{$hook}}, $code; | ||
| 14 | } | ||
| 15 | } else { | ||
| 16 | no strict 'refs'; | ||
| 17 | while (my ($hook, $code) = each %args) { | ||
| 18 | next unless ref $code eq 'CODE'; | ||
| 19 | push @{${"${class}::_trigger"}->{$hook}}, $code; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | sub call_trigger { | ||
| 25 | my ($class, $hook, @args) = @_; | ||
| 26 | my @code = $class->get_trigger_code($hook); | ||
| 27 | for my $code (@code) { | ||
| 28 | $code->(@args); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | sub get_trigger_code { | ||
| 33 | my ($class, $hook) = @_; | ||
| 34 | my @code; | ||
| 35 | if (Scalar::Util::blessed($class)) { | ||
| 36 | push @code, @{ $class->{_trigger}->{$hook} || [] }; | ||
| 37 | $class = ref $class; | ||
| 38 | } | ||
| 39 | no strict 'refs'; | ||
| 40 | my $klass = ref $class || $class; | ||
| 41 | push @code, @{${"${klass}::_trigger"}->{$hook} || []}; | ||
| 42 | return @code; | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; |