| 1 | package Tk::AppWindow::BaseClasses::Callback; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Tk::AppWindow::BaseClasses::Callback - providing callbacks | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | $VERSION="0.21"; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Data::Compare; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my $cb = Tk::AppWindow::BaseClasses::Callback->new('MethodName', $owner, @options); | ||||||||||||||||||||||||||||
| 22 | my $cb = Tk::AppWindow::BaseClasses::Callback->new(sub { do whatever }, @options); | ||||||||||||||||||||||||||||
| 23 | $cb->execute(@moreoptions); | ||||||||||||||||||||||||||||
| 24 | $cb->hookBefore('some_method', $obj, @param); | ||||||||||||||||||||||||||||
| 25 | $cb->hookBefore(\&some_sub, @param); | ||||||||||||||||||||||||||||
| 26 | $cb->unhookBefore('some_method', $obj, @param); | ||||||||||||||||||||||||||||
| 27 | $cb->unhookBefore(\&some_sub, @param); | ||||||||||||||||||||||||||||
| 28 | $cb->hookAfter('some_method', $obj, @param); | ||||||||||||||||||||||||||||
| 29 | $cb->hookAfter(\&some_sub, @param); | ||||||||||||||||||||||||||||
| 30 | $cb->unhooAfter('some_method', $obj, @param); | ||||||||||||||||||||||||||||
| 31 | $cb->unhookAfter(\&some_sub, @param); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | This module provides means to create universal callbacks. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | After creation it can hook and unhook other callbacks to it. | ||||||||||||||||||||||||||||
| 38 | Those hooked through the B<hookBefore> method will be called before the main callback. | ||||||||||||||||||||||||||||
| 39 | Those hooked through the B<hookAfter> method will be called after the main callback. | ||||||||||||||||||||||||||||
| 40 | Results are passed forward through the chain. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 METHODS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =over 4 | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item B<new> | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | There are two ways to create a new callback; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | my $c = Tk::AppWindow::BaseClasses::Callback->new('MethodName', $owner, @options); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | When you call B<execute> the options you pass to it will be placed after $owner and before @options | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | my $c = Tk::AppWindow::BaseClasses::Callback->new(\&SomeAnonymusSub, @options); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | When you call B<execute> the options you pass to it will be placed after @options | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 2 | my $proto = shift; | |||||||||||||||||||||||||||
| 64 | 2 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 65 | 2 | my $self = {}; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | $self->{CMD} = []; | |||||||||||||||||||||||||||
| 68 | 2 | $self->{HOOKSAFTER} = []; | |||||||||||||||||||||||||||
| 69 | 2 | $self->{HOOKSBEFORE} = []; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | bless ($self, $class); | |||||||||||||||||||||||||||
| 72 | 2 | $self->{CMD} = [@_]; #if $self->Check(@_); | |||||||||||||||||||||||||||
| 73 | 2 | return $self; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 98 | ▶ | sub Callback { | ||||||||||||||||||||||||||
|
Subroutine
Callback: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 98 | my ($self, $cmd, @options) = @_; | |||||||||||||||||||||||||||
| 78 | 98 | ▶ | croak 'Command not defined' unless defined $cmd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 98 | my @call = @$cmd; | |||||||||||||||||||||||||||
| 80 | 98 | my $sub = shift @call; | |||||||||||||||||||||||||||
| 81 | 98 | my @opt = (); | |||||||||||||||||||||||||||
| 82 | 98 | ▶ | unless ((ref $sub) and ($sub =~/^CODE/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $sub and $sub =~ /^CODE/
MC/DC50%1/2
ref $sub and $sub =~ /^CODE/
ref $sub
$sub =~ /^CODE/
Truth table66%2/3
A and B
A= ref $subB= $sub =~ /^CODE/
| |||||||||||||||||||||||||||||
| 83 | 49 | my $owner = shift @call; | |||||||||||||||||||||||||||
| 84 | 49 | my $call = $owner->can($sub); | |||||||||||||||||||||||||||
| 85 | 49 | ▶ | unless (defined $call) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | croak "Method $call not found on object $owner"; | |||||||||||||||||||||||||||
| 87 | return undef | ||||||||||||||||||||||||||||
| 88 | 0 | } | |||||||||||||||||||||||||||
| 89 | 49 | return &$call($owner, @call, @options); | |||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 49 | return &$sub(@call, @options); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 12 | ▶ | sub Check { | ||||||||||||||||||||||||||
|
Subroutine
Check: called CC=7 SCAR=23.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 97 | 12 | my $call = shift; | |||||||||||||||||||||||||||
| 98 | 12 | ▶ | unless ((ref $call) and ($call =~/^CODE/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $call and $call =~ /^CODE/
MC/DC50%1/2
ref $call and $call =~ /^CODE/
ref $call
$call =~ /^CODE/
Truth table66%2/3
A and B
A= ref $callB= $call =~ /^CODE/
| |||||||||||||||||||||||||||||
| 99 | 6 | my $owner = shift; | |||||||||||||||||||||||||||
| 100 | 6 | ▶ | unless (defined $owner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | carp "no owner defined"; | |||||||||||||||||||||||||||
| 102 | 0 | return 0 | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 6 | ▶ | unless ((blessed $owner) and ($owner =~ /^\S+\=/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($owner) and $owner =~ /^\S+\=/
MC/DC0%0/2
builtin::blessed($owner) and $owner =~ /^\S+\=/
builtin::blessed($owner)
$owner =~ /^\S+\=/
Truth table33%1/3
A and B
A= builtin::blessed($owner)B= $owner =~ /^\S+\=/
| |||||||||||||||||||||||||||||
| 105 | 0 | carp "not an object"; | |||||||||||||||||||||||||||
| 106 | 0 | return 0 | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 6 | ▶ | unless ($owner->can($call)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | carp "invalid method: $call"; | |||||||||||||||||||||||||||
| 110 | 0 | return 0; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 12 | return 1; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item B<execute>(I<@options>) | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Runs the callback and returns the result. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 26 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 26 | my @param = @_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 26 | my $before = $self->{HOOKSBEFORE}; | |||||||||||||||||||||||||||
| 127 | 26 | for (@$before) { | |||||||||||||||||||||||||||
| 128 | 36 | $self->Callback($_, @param); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 26 | my @result = $self->Callback($self->{CMD}, @param); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 26 | my $after = $self->{HOOKSAFTER}; | |||||||||||||||||||||||||||
| 134 | 26 | for (@$after) { | |||||||||||||||||||||||||||
| 135 | 36 | $self->Callback($_, @result); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 26 | ▶ | return if @result eq 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 26 | ▶ | return $result[0] if @result eq 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | return @result | ||||||||||||||||||||||||||||
| 140 | 0 | } | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =item B<hookAfter>I<(@callback)> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Adds a hook to the after section. The items in I<@callback> are exactly as creating a new instance of this package. | ||||||||||||||||||||||||||||
| 145 | The callback will be called after the main callback is fed with what the main callback returns as parameters. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 6 | ▶ | sub hookAfter { | ||||||||||||||||||||||||||
|
Subroutine
hookAfter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 151 | 6 | my $hk = $self->{HOOKSAFTER}; | |||||||||||||||||||||||||||
| 152 | 6 | $self->Check(@_); | |||||||||||||||||||||||||||
| 153 | 6 | push @$hk, [@_]; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item B<hookBefore>(I<@callback>) | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Adds a hook to the before section. The items in I<@callback> are exactly as creating a new instance of this package. | ||||||||||||||||||||||||||||
| 159 | The callback will be called before the main callback and is fed with the same parameters. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 6 | ▶ | sub hookBefore { | ||||||||||||||||||||||||||
|
Subroutine
hookBefore: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 165 | 6 | my $hk = $self->{HOOKSBEFORE}; | |||||||||||||||||||||||||||
| 166 | 6 | $self->Check(@_); | |||||||||||||||||||||||||||
| 167 | 6 | push @$hk, [@_]; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item B<unhookAfter>I<(@callback)> | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Removes a hook from the after section. The items in I<@callback> are exactly as when adding the hook. | ||||||||||||||||||||||||||||
| 174 | If multiple identical items are present it removes them all. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 6 | ▶ | sub unhookAfter { | ||||||||||||||||||||||||||
|
Subroutine
unhookAfter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 180 | 6 | my $hook = [ @_ ]; | |||||||||||||||||||||||||||
| 181 | 6 | my $found = 0; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 6 | my $after = $self->{HOOKSAFTER}; | |||||||||||||||||||||||||||
| 184 | 6 | my @na = (); | |||||||||||||||||||||||||||
| 185 | 6 | for (@$after) { | |||||||||||||||||||||||||||
| 186 | 12 | ▶ | unless (Compare($_, $hook)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 6 | push @na, $_; | |||||||||||||||||||||||||||
| 188 | } else { | ||||||||||||||||||||||||||||
| 189 | 6 | $found = 1; | |||||||||||||||||||||||||||
| 190 | # last; | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 6 | $self->{HOOKSAFTER } = \@na; | |||||||||||||||||||||||||||
| 194 | 6 | ▶ | carp "Hook not found" unless $found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =item B<unhookBefore>I<(@callback)> | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Removes a hook from the before section. The items in I<@callback> are exactly as when adding the hook. | ||||||||||||||||||||||||||||
| 201 | If multiple identical items are present it removes them all. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 6 | ▶ | sub unhookBefore { | ||||||||||||||||||||||||||
|
Subroutine
unhookBefore: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 207 | 6 | my $hook = [ @_ ]; | |||||||||||||||||||||||||||
| 208 | 6 | my $found = 0; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 6 | my $before = $self->{HOOKSBEFORE}; | |||||||||||||||||||||||||||
| 211 | 6 | my @nb = (); | |||||||||||||||||||||||||||
| 212 | 6 | for (@$before) { | |||||||||||||||||||||||||||
| 213 | 12 | ▶ | unless (Compare($_, $hook)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 6 | push @nb, $_; | |||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||
| 216 | 6 | $found = 1; | |||||||||||||||||||||||||||
| 217 | # last; | ||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 6 | $self->{HOOKSBEFORE } = \@nb; | |||||||||||||||||||||||||||
| 221 | 6 | ▶ | carp "Hook not found" unless $found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =back | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Hans Jeuken (hanje at cpan dot org) | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 BUGS | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Unknown. If you find any, please contact the author. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =cut | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 1 | 1; | |||||||||||||||||||||||||||
| 237 | __END__ | ||||||||||||||||||||||||||||