如何在 SO q 2621225 中调用同一个 Perl 文件中的包
How can I call a package in the same Perl file as in SO q 2621225
我正在学习 Higher Order Perl,并想尝试执行它的一些代码,在本例中为 FlatDB.pm
.
我试图模拟 this question (2621225) 的答案中概述的调用方法,但它对我不起作用。即:
## HOP Chapter 4 section 3.4, p.140
my $FIELDSEP = qr/:/;
package FlatDB;
sub new {
my $class = shift;
my $file = shift;
open my $fh, "< $file" or return;
chomp(my $schema = <$fh>);
my @field = split $FIELDSEP, $schema;
my %fieldnum = map { uc $field[$_] => $_ } (0..$#field);
print "\nfieldnum=",%fieldnum;
bless { FH => $fh, FIELDS => \@field, FIELDNUM => \%fieldnum,
FIELDSEP => $FIELDSEP } => $class;
}
# More subs here - snipped
我为尝试 运行 包添加的内容:
package main;
print "\nat 89";
$obj= FlatDB->new("FlatDB","SampleDB.txt");
print "\nat 91";
执行 89 和 91 处的打印,但不执行 'new' 子例程中的打印。如果我将 'new' 子例程从包中取出,它就可以工作,所以问题一定出在我如何调用它上。
这恐怕是很简单的事情,但我没有看到。
方法可以退出而不执行 print
语句的唯一方法是通过行
open my $fh, "< $file" or return;
所以我想 open
由于某种原因失败了。将该行替换为
open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};
你会看到失败的原因
我正在学习 Higher Order Perl,并想尝试执行它的一些代码,在本例中为 FlatDB.pm
.
我试图模拟 this question (2621225) 的答案中概述的调用方法,但它对我不起作用。即:
## HOP Chapter 4 section 3.4, p.140
my $FIELDSEP = qr/:/;
package FlatDB;
sub new {
my $class = shift;
my $file = shift;
open my $fh, "< $file" or return;
chomp(my $schema = <$fh>);
my @field = split $FIELDSEP, $schema;
my %fieldnum = map { uc $field[$_] => $_ } (0..$#field);
print "\nfieldnum=",%fieldnum;
bless { FH => $fh, FIELDS => \@field, FIELDNUM => \%fieldnum,
FIELDSEP => $FIELDSEP } => $class;
}
# More subs here - snipped
我为尝试 运行 包添加的内容:
package main;
print "\nat 89";
$obj= FlatDB->new("FlatDB","SampleDB.txt");
print "\nat 91";
执行 89 和 91 处的打印,但不执行 'new' 子例程中的打印。如果我将 'new' 子例程从包中取出,它就可以工作,所以问题一定出在我如何调用它上。
这恐怕是很简单的事情,但我没有看到。
方法可以退出而不执行 print
语句的唯一方法是通过行
open my $fh, "< $file" or return;
所以我想 open
由于某种原因失败了。将该行替换为
open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};
你会看到失败的原因