【发布时间】:2015-03-12 02:29:54
【问题描述】:
我需要包装一个子程序,以便它可以在返回其原始返回值之前触发之前/之后的事件。一切正常,除非 coderef 返回一个数组,它将被强制转换为 arrayref。有时我确实想要一个数组引用,所以我不能只检查引用类型并重铸为数组。我也尝试过使用wantarray,它仍然返回一个arrayref。
我目前正在尝试这样做
$owner->methods->each(sub { # Methods is a hash container where each entry is a mapping of a method name to its implementation (as a coderef)
my ($key, $val) = @_;
return if $key eq 'trigger' || $key eq 'triggerBefore' || $key eq 'before' || $key eq 'on'; # Ignore decorating keys that would cause infinite callbacks
$owner->methods->set($key, sub { # Replace the original sub with a version that calls before and after events
my ($obj, @args) = @_;
$owner->triggerBefore($key, @args);
my $return = $val->(@args); # Return could be anything...
$owner->trigger($key, $return);
return $return;
});
});
我也尝试用以下内容替换退货,但无济于事:
return (wantarray && ref $return eq 'ARRAY') ? @$return : $return;
如果我不存储返回值而是return $val->(@args);(但随后我失去了“之后”触发器),一切正常。有没有办法“按原样”存储返回值而不是将其存储在标量中?
【问题讨论】:
-
Contextual::Return可能值得一看
标签: perl