/usr/share/perl5/Spoon/Hooks.pm is in libspoon-perl 0.24-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | package Spoon::Hooks;
use Spoon::Base -Base;
const hook_class => 'Spoon::Hook';
const hooked_class => 'Spoon::Hooked';
sub add {
my ($target, %hooks) = @_;
my $original = $self->assert_method($target);
my $pre = $self->assert_method($hooks{pre});
my $post = $self->assert_method($hooks{post});
my $replacement = $self->new_hook_sub($original, $pre, $post);
my $hook_point = $self->get_full_name($target);
no warnings 'redefine';
no strict 'refs';
*$hook_point = $replacement;
return $self->hooked_class->new(
hook_point => $hook_point,
original => $original,
replacement => $replacement,
);
}
sub new_hook_sub {
my ($original, $pre, $post) = @_;
sub {
my $hook = $self->hook_class->new(
code => $original,
pre => $pre,
post => $post,
);
$hook->returned([$hook->pre->(@_, $hook)])
if $pre;
my $code = $hook->code
or return $hook->returned;
my $new_args = $hook->new_args;
@_ = @$new_args
if $new_args;
$hook->returned([&$code(@_)]);
return $hook->post->(@_, $hook)
if $hook->post;
return $hook->returned;
}
}
sub assert_method {
return shift
if not defined($_[0]) or ref($_[0]);
my $full_name = $self->get_full_name(shift);
my ($package, $method) = ($full_name) =~ /(.*)::(.*)/
or die "Can't hook invalid fully qualified method name: '$full_name'";
unless ($package->can('new')) {
eval "require $package";
undef($@);
die "Can't hook $full_name. Can't find package '$package'"
unless $package->can('new');
}
my $sub = $full_name;
return \&$sub if defined &$sub;
no strict 'refs';
*$sub = eval <<END;
sub {
package $package;
my \$self = shift;
\$self->SUPER::$method(\@_);
};
END
return \&$sub;
}
sub get_full_name {
my $name = shift;
return $name if $name =~ /::/;
if ($name =~ /(.*):(.*)/) {
my ($class_id, $method) = ($1, $2);
my $package = $self->hub->registry->lookup->classes->{$class_id};
return $package . '::' . $method;
}
return '';
}
package Spoon::Hooked;
use Spoon::Base -Base;
field 'hook_point';
field 'original';
field 'replacement';
sub unhook {
my ($hook_point, $original, $replacement) =
@{$self}{qw(hook_point original replacement)};
%$self = ();
return unless defined $hook_point;
no strict 'refs';
my $current = *$hook_point{CODE};
die "Unhooking error for $hook_point"
unless "$current" eq "$replacement";
no warnings;
*$hook_point = $original;
return 1;
}
sub DESTROY {
$self->unhook;
}
package Spoon::Hook;
use Spoon::Base -Base;
field 'code';
field 'pre';
field 'post';
field 'new_args';
sub returned {
$self->{returned} = shift if @_;
$self->{returned} ||= [];
wantarray ? (@{$self->{returned}}) : $self->{returned}[0];
}
sub returned_true {
@{$self->{returned}} && $self->{returned}[0] && 1;
}
sub cancel {
$self->code(undef);
return ();
}
__END__
=head1 NAME
Spoon::Hook - Spoon Method Hooking Facility
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 AUTHOR
Brian Ingerson <INGY@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2004. Brian Ingerson. All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
=cut
|