什么是 Perl 内置运算符/函数? [英] What are Perl built-in operators/functions?
问题描述
我正在阅读 Simon 的Beginning PerlCozens 和在第 8 章 - 子程序中他指出子程序"是用户函数,而 print
、open
、split
> 等是内置的运算符或函数.
I'm reading Beginning Perl by Simon Cozens and in Chapter 8 - Subroutines he states that "subroutines" are user functions, while print
, open
, split
, etc. are built-in operators or functions.
它们是什么?它们真的是内置的、语言内在的特性(比如 C 的 sizeof
运算符)还是它们实际上是 main
模块的子例程/函数?
What are they? Are they really built-in, language-intrinsic features (like C's sizeof
operator) or are they, actually, subroutines/functions of the main
module?
如果它们是子例程,while
、for
、unless
等是否也是子例程?+
、-
、eq
等操作符怎么样?
If they're subroutines, are while
, for
, unless
, etc. also subroutines? What about the operators like +
, -
, eq
, etc.?
推荐答案
print
, open
, split
不是子程序.它们不会导致子调用.它们甚至不存在于符号表中(在 main::
或其他地方,尽管您可以将它们称为 CORE::split
等),并且无法获得对他们代码的引用(尽管在 CORE::
中正在为他们创建代理子程序,以便您将它们视为子程序时).它们就像 +
一样是运算符.
print
, open
, split
are not subroutines. They do not result in sub calls. They are not even present in the symbol table (in main::
or otherwise, although you can refer to them as CORE::split
, etc), and one cannot get a reference to their code (although work is being done to create proxy subs for them in CORE::
for when you want to treat them as subroutines). They are operators just like +
.
$ perl -MO=Concise,-exec -e'sub f {} f()'
1 <0> enter
2 <;> nextstate(main 2 -e:1) v:{
3 <0> pushmark s
4 <#> gv[*f] s
5 <1> entersub[t3] vKS/TARG,1 <--- sub call
6 <@> leave[1 ref] vKP/REFC
-e syntax OK
$ perl -MO=Concise,-exec -e'split /;/'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 </> pushre(/";"/) s/64
4 <#> gvsv[*_] s
5 <$> const[IV 0] s
6 <@> split[t2] vK <--- not a sub call
7 <@> leave[1 ref] vKP/REFC
-e syntax OK
$ perl -MO=Concise,-exec -e'$x + $y'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <#> gvsv[*x] s
4 <#> gvsv[*y] s
5 <2> add[t3] vK/2 <--- Just like this
6 <@> leave[1 ref] vKP/REFC
-e syntax OK
它们有多种名称:
- 内置函数
- 功能
- 内置函数
- 命名运算符
大多数都被认为是以下之一:
And most are considered to be one of the following:
- 列表运算符
- 命名一元运算符
子程序通常被称为函数(就像在 C 和 C++ 中一样),因此函数"是一个含糊不清的词.这种歧义似乎是您提出问题的基础.
Subroutines are often called functions (as they are in C and C++), so "function" is an ambiguous word. This ambiguity appears to be the basis of your question.
对于while
、for
、unless
等,都是流程控制语句使用的关键字
while (f()) { g() }
和语句修饰符
g() while f();
这篇关于什么是 Perl 内置运算符/函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!