Perl 方法调用可以被拦截吗? [英] Can Perl method calls be intercepted?

查看:54
本文介绍了Perl 方法调用可以被拦截吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能在 Perl 中拦截一个方法调用,用参数做一些事情,然后执行它吗?

Can you intercept a method call in Perl, do something with the arguments, and then execute it?

推荐答案

是的,您可以拦截 Perl 子例程调用.我在 掌握 Perl 中有关于这类事情的一整章.查看 Hook::LexWrap 模块,它让您无需浏览所有内容即可完成的细节.Perl 的方法只是子程序.

Yes, you can intercept Perl subroutine calls. I have an entire chapter about that sort of thing in Mastering Perl. Check out the Hook::LexWrap module, which lets you do it without going through all of the details. Perl's methods are just subroutines.

您还可以创建子类并覆盖您想要捕获的方法.这是一种稍微好一点的方法,因为这就是面向对象编程希望您这样做的方式.但是,有时人们编写的代码不允许您正确执行此操作.在掌握 Perl 中还有更多相关内容.

You can also create a subclass and override the method you want to catch. That's a slightly better way to do it because that's the way object-oriented programming wants you do to it. However, sometimes people write code that doesn't allow you to do this properly. There's more about that in Mastering Perl too.

这篇关于Perl 方法调用可以被拦截吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆