从同一文件中定义的包导入符号
我希望我可以做这样的事情:
p.pl :
package Common;
use strict;
use warnings;
use experimental qw(signatures);
use Exporter qw(import);
our @EXPORT = qw(NA);
sub NA() { "NA" }
package Main;
use feature qw(say);
use strict;
use warnings;
use experimental qw(signatures);
Common->import();
say "Main: ", NA();
my $client = Client->new();
$client->run();
package Client;
use feature qw(say);
use strict;
use warnings;
use experimental qw(signatures);
Common->import();
sub run($self) {
say "Client: ", NA();
}
sub new( $class, %args ) { bless \%args, $class }
在同一文件中的两个包之间共享公共符号。然而运行这个脚本会给出:
$ perl p.pl
Main: NA
Undefined subroutine &Client::NA called at ./p.pl line 30.
我在这里缺少什么?
I hoped I could do something like this:
p.pl :
package Common;
use strict;
use warnings;
use experimental qw(signatures);
use Exporter qw(import);
our @EXPORT = qw(NA);
sub NA() { "NA" }
package Main;
use feature qw(say);
use strict;
use warnings;
use experimental qw(signatures);
Common->import();
say "Main: ", NA();
my $client = Client->new();
$client->run();
package Client;
use feature qw(say);
use strict;
use warnings;
use experimental qw(signatures);
Common->import();
sub run($self) {
say "Client: ", NA();
}
sub new( $class, %args ) { bless \%args, $class }
to share common symbols between two packages in the same file. However running this script gives:
$ perl p.pl
Main: NA
Undefined subroutine &Client::NA called at ./p.pl line 30.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您
之前
调用了A simple way to inline module:
然后您可以正常使用
use Common;
。它并不完美。像 App::FatPacker 这样连接到
@INC
确实可以提供最佳结果。但这会让你的生活更轻松。The problem is that you call
before
A simple way to inline modules:
Then you can use
use Common;
as normal.It's not perfect. Hooking into
@INC
like App::FatPacker does provides the best results. But it will make your life easier.