在 Perl 中,有多少种方法可以调用子程序而忽略其原型? [英] How many ways can you call a subroutine and ignore its prototype in Perl?

查看:38
本文介绍了在 Perl 中,有多少种方法可以调用子程序而忽略其原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都应该熟悉与 Perl 中的原型相关的问题.这是两个大问题:

We should all be familiar with the problems related to prototypes in Perl. Here are the two biggies:

  • 它们不像其他语言的原型那样工作,所以人们会误解它们.
  • 它们并不适用于所有调用子程序的方式.

第二个是我目前比较好奇的.

The second item is the one I am curious about at the moment.

我知道有两种方法可以在调用子例程时颠覆/解决/忽略原型执行:

I know of two ways to subvert/work around/ignore prototype enforcement when calling a subroutine:

  • 将子作为方法调用.Foo->subroutine_name();
  • 使用前导 & 符号调用 sub.&subroutine_name();
  • Call the sub as a method. Foo->subroutine_name();
  • Call the sub with a leading & sigil. &subroutine_name();

我还遗漏了其他有趣的案例吗?

Are there any other interesting cases I've missed?

更新:

@brian d foy,我并不是特别想逃避原型,但我想知道有多少种方法可以做到?"我出于好奇问这个问题.

@brian d foy, I don't particularly want to evade prototypes, but I wondered "how many ways are there to do it?" I ask this question out of curiosity.

@jrockway,我同意你的观点,我相信你已经更明确、更简洁地描述了我关于原型问题的第一点,人们误解了它们.也许问题在于程序员的期望而不是功能.但这确实是一个我不想要的哲学问题.

@jrockway, I agree with you, and I believe that you have more explicitly and more concisely described my first point regarding the problems with prototypes, that people misunderstand them. Perhaps the problem lies in programmer expectations and not in the feature. But that is really a philosophical question of the sort I don't want to have.

推荐答案

通过子程序引用调用.

sub foo($) { print "arg is $_[0]\n" }
my $sub = \&foo;
$sub->();

<小时>

在 Perl 看到原型之前调用它(很重要,因为 perl 不会让你在使用前声明 subs):


Call it before Perl has seen the prototype (important because perl doesn't make you declare subs before use):

foo();
sub foo($) { print "arg is $_[0]\n" }

这篇关于在 Perl 中,有多少种方法可以调用子程序而忽略其原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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