我如何引用 Perl 子例程?

发布于 2024-08-28 05:15:22 字数 347 浏览 7 评论 0原文

我在弄清楚如何引用外部模块文件中的子例程时遇到了一些麻烦。现在,我正在这样做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-09-04 05:15:22

正如 perlmodlib 中所述,您的模块名称应该以大写字母开头:

Perl 非正式地为“pragma”模块保留小写模块名称,例如 integerstrict。其他模块通常以大写字母开头,并使用不带下划线的混合大小写(需要简短且可移植)。

调用另一个包中定义的子程序的一种方法是在调用它时完全限定该子程序的名称:

SettingsGeneral::printScreen "important message\n";

如果您想要的只是对 printScreen 的引用,请使用反斜杠运算符获取它

my $subref = \&SettingsGeneral::printScreen;

并使用一个调用它您

&$subref("one\n");
&{$subref}("two\n");
$subref->("three\n");

可以在当前包中创建一个 别名

*printScreen = \&SettingsGeneral::printScreen;
printScreen("another urgent flash\n");

跳过括号 (必要的,因为当前包中的子在编译时未知)通过编写:

use subs 'printScreen';
*printScreen = \&SettingsGeneral::printScreen;
printScreen "the sky is falling!\n";

Exporter模块可以为您完成此保管工作:

SettingsGeneral.pm:

package SettingsGeneral;

use Exporter 'import';

our @EXPORT = qw/ printScreen /;

sub printScreen {
  print $_[0];
}

1;

ma​​in:

#! /usr/bin/perl

use warnings;
use strict;

use SettingsGeneral;

printScreen "foo!\n";

As noted in perlmodlib, you should start your module's name with an uppercase letter:

Perl informally reserves lowercase module names for 'pragma' modules like integer and strict. Other modules normally begin with a capital letter and use mixed case with no underscores (need to be short and portable).

One way to call a sub defined in another package is to fully qualify that sub's name when you call it:

SettingsGeneral::printScreen "important message\n";

If all you want is a reference to printScreen, grab it with the backslash operator

my $subref = \&SettingsGeneral::printScreen;

and call it with one of

&$subref("one\n");
&{$subref}("two\n");
$subref->("three\n");

You could create an alias in your current package:

*printScreen = \&SettingsGeneral::printScreen;
printScreen("another urgent flash\n");

Skip the parentheses (necessary because the sub in the current package wasn't known at compile time) by writing:

use subs 'printScreen';
*printScreen = \&SettingsGeneral::printScreen;
printScreen "the sky is falling!\n";

The Exporter module can do this custodial work for you:

SettingsGeneral.pm:

package SettingsGeneral;

use Exporter 'import';

our @EXPORT = qw/ printScreen /;

sub printScreen {
  print $_[0];
}

1;

main:

#! /usr/bin/perl

use warnings;
use strict;

use SettingsGeneral;

printScreen "foo!\n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文