如何将函数附加到 class 函数

How to append functions to class functions

我正在写一个网页API,它有几个功能,例如:登录、注销、摘要等等。

每次我调用这些函数时,我都会检查我将发布的数据转换为 JSON 并检查护照是否过期

如何在每次调用 api 对象的函数时预先附加一个执行这两件事的函数

我想Sub::Prepend可能对你有帮助。

样本:

use Sub::Prepend 'prepend';

    sub foo ($) {
        print "Foo executed with \@_ = (@_).\n";
    }

    BEGIN {
        prepend foo => sub {
            # This is called before foo executes.
            print "Foo was called with \@_ = (@_).\n";
            push @_, 'and more';
        }
    }

    my @bar = qw/ foo bar baz /;
    foo(@bar); # The prototype is preserved!

    __END__
    Foo was called with @_ = (3).
    Foo executed with @_ = (3 and more).