perl - 如何在 perl 中的每个类方法之后踢一个特定的回调子程序?

标签 perl oop callback aggregation

假设我已经拥有了带有多个子例程的 Child 包和 Parent 包。这两个包通过聚合组合在一起,就像在 perltoot 中一样:

use warnings;
use strict;

package Child;

sub new {
    my ($class, %arg) = @_;
    return bless { %arg }, $class;
}

sub method_x {
    warn 'call method x';
}

sub method_y {
    warn 'call method y';
}

sub method_z {
    warn 'call method z';
}

1;


package Parent;

sub new {
    my ($class, %arg) = @_;
    return bless {
        child => undef,
        %arg,
    }, $class;
}

sub child { shift->{child} }
sub x { shift->child->method_x(@_) }
sub y { shift->child->method_y(@_) }
sub z { shift->child->method_z(@_) }

sub _callback {
    warn "I want to kick this callback after every child methods.";
}

1;


package main;

my $p = Parent->new(
    child => Child->new,
);

$p->x;
$p->y;
$p->z;

1;

过了一会儿,我想为每个 Child 的方法启动 _callback,我惊呆了,因为我试图将这个回调添加到每个包装器方法(x/y/z).

我可以更优雅地完成这项工作吗?我是否必须在一开始就允许包有更大的灵 active ?怎么办?

如有任何建议,我们将不胜感激。

最佳答案

一种可能性是使用方法修饰符,它由MooseMoo 等对象系统提供:

use strict; use warnings;

package Child {
  use Moose;

  sub method_x { warn "call method_x" }
  sub method_y { warn "call method_y" }
  sub method_z { warn "call method_z" }
}

package Parent {
  use Moose;

  has child => (is => 'rw');

  sub x { shift->child->method_x(@_) }
  sub y { shift->child->method_y(@_) }
  sub z { shift->child->method_z(@_) }

  # A method modifier in action
  after [qw/ x y z /] => sub {
    warn "called after every Parent (!) invocation";
  };
}

my $p = Parent->new(child => Child->new);

$p->x; $p->y; $p->z;

输出:

call method_x at - line 7.
called after every Parent (!) invocation at - line 23.
call method_y at - line 8.
called after every Parent (!) invocation at - line 23.
call method_z at - line 9.
called after every Parent (!) invocation at - line 23.

如果您真的希望包装 所有 Child 的方法,请使用子类:

package WrappedChild {
  use Moose;
  extends 'Child';

  # the /(?=)/ regex matches always
  after qr/(?=)/ => sub {
    warn "called after each method in Child";
  };
}


my $p = Parent->new(child => WrappedChild->new);

$p->x; $p->y; $p->z;

这产生

called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
call method_x at - line 7.
called after each method in Child at - line 32.
called after every Parent (!) invocation at - line 22.
call method_y at - line 8.
called after each method in Child at - line 32.
called after every Parent (!) invocation at - line 22.
call method_z at - line 9.
called after each method in Child at - line 32.
called after every Parent (!) invocation at - line 22.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.

这可能有点过分。坚持使用明确的名称可能更可取。

参见 Moose::Manual::MethodModifiers了解更多信息。


如果你不想使用任何模块,你可以通过 jungle 符号表破解你的方式:

for my $name (qw/method_x method_y method_z/) {
  no strict 'refs';
  no warnings 'redefine';
  my $orig = \&{"Child::$name"};
  *{"Child::$name} = sub {
    my @return_values = wantarray ? $orig->() : scalar $orig->();
    warn "called after each method";
    return wantarray ? @return_values : $return_values[0];
  };
}

输出:

call method_x at - line 7.
called after each method at - line 31.
call method_y at - line 8.
called after each method at - line 31.
call method_z at - line 9.
called after each method at - line 31.

关于perl - 如何在 perl 中的每个类方法之后踢一个特定的回调子程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19794654/

相关文章:

perl - 在 Perl 中使用 die 时的退出代码

正则表达式:忽略几个不同位置上的字符

javascript - 查询对象数组javascript

java - 无法为接口(interface)实现者创建泛型

linux - 从单行中提取校验和

perl - Perl 子程序中的变量不会释放内存

php - 您认为这是对类文件的愚蠢使用吗?

javascript - 从孙子中的 onClick 更新 React 祖 parent 状态

Android 回调不会在应用程序 onStop/OnResume 后再次运行

c - 使用 C 中的回调注册事件