您可以参考Perl中的内置函数吗? [英] Can you take a reference of a builtin function in Perl?

查看:96
本文介绍了您可以参考Perl中的内置函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么语法(如果有的话)能够引用诸如shift之类的内置函数?

What syntax, if any, is able to take a reference of a builtin like shift?

$shift_ref = $your_magic_syntax_here;

使用相同的方式创建用户定义的子项:

The same way you could to a user defined sub:

sub test { ... }

$test_ref = \&test;

我已经尝试了以下所有方法,但均无效:

I've tried the following, which all don't work:

\&shift
\&CORE::shift
\&{'shift'}
\&{'CORE::shift'}

如果需要,您的答案可以包含XS,但我不希望这样.

Your answer can include XS if needed, but I'd prefer not.

说明:我正在寻找一种通用解决方案,该解决方案可以从任何内置函数中获取功能齐全的代码参考.然后可以将此代码引用传递给任何更高阶的函数,就像对用户定义的子对象的引用一样.到目前为止,似乎不可能达成共识,有人愿意反对吗?

Clarification: I am looking for a general purpose solution that can obtain a fully functional code reference from any builtin. This coderef could then be passed to any higher order function, just like a reference to a user defined sub. It seems to be the consensus so far that this is not possible, anyone care to disagree?

推荐答案

不,您不能.您要解决的根本问题是什么?可能有某种方法可以做任何事情.

No, you can't. What is the underlying problem you are trying to solve? There may be some way to do whatever that is.

添加问题您的答案可以根据需要包含XS,但我不希望包含XS"的问题的补充部分, 从XS调用内置函数真的很困难,因为内置函数被设置为假定它们作为已编译optree的一部分运行并且设置了一些全局变量.通常,调用内置函数本身使用的某些底层函数要容易得多,尽管并不总是这样,所以您会看到类似这样的东西:

Re the added part of the question "Your answer can include XS if needed, but I'd prefer not.", calling builtins from XS is really hard, since the builtins are set up to assume they are running as part of a compiled optree and have some global variables set. Usually it's much easier to call some underlying function that the builtin itself uses, though there isn't always such a function, so you see things like:

buffer = sv_2mortal(newSVpvf("(caller(%d))[3]", (int) frame));
caller = eval_pv(SvPV_nolen(buffer), 1);

(从XS进行字符串评估,而不是直接调用pp_caller所需的箍).

(doing a string eval from XS rather than go through the hoops required to directly call pp_caller).

这篇关于您可以参考Perl中的内置函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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