Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

syntax - Can you take a reference of a builtin function in Perl?

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'}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...