我如何引用 Perl 子例程?
我在弄清楚如何引用外部模块文件中的子例程时遇到了一些麻烦。现在,我正在这样做:
External file
package settingsGeneral;
sub printScreen {
print $_[0];
}
Main
use settingsGeneral;
my $printScreen = settingsGeneral::printScreen;
&$printScreen("test");
但这会导致错误: 使用“严格引用”时不能使用字符串(“1”)作为子例程引用
I'm having some trouble figuring out how to make a reference to a subroutine in an external module file. Right now, I'm doing this:
External file
package settingsGeneral;
sub printScreen {
print $_[0];
}
Main
use settingsGeneral;
my $printScreen = settingsGeneral::printScreen;
&$printScreen("test");
but this result into an error:
Can't use string ("1") as a subroutine ref while "strict refs" in use
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 perlmodlib 中所述,您的模块名称应该以大写字母开头:
调用另一个包中定义的子程序的一种方法是在调用它时完全限定该子程序的名称:
如果您想要的只是对
printScreen
的引用,请使用反斜杠运算符获取它并使用一个调用它您
可以在当前包中创建一个 别名:
跳过括号 (必要的,因为当前包中的子在编译时未知)通过编写:
Exporter模块可以为您完成此保管工作:
SettingsGeneral.pm:
main:
As noted in perlmodlib, you should start your module's name with an uppercase letter:
One way to call a sub defined in another package is to fully qualify that sub's name when you call it:
If all you want is a reference to
printScreen
, grab it with the backslash operatorand call it with one of
You could create an alias in your current package:
Skip the parentheses (necessary because the sub in the current package wasn't known at compile time) by writing:
The Exporter module can do this custodial work for you:
SettingsGeneral.pm:
main: