我创建了一个Perl类/模块来显示圣经经文。其中有一个哈希表,其中存储了几个诗句,关键字是书/章节/诗句,值是文本。这个散列是从模块返回的。
我将“圣经”类包含在一个控制器类中,这种连接似乎起作用了。问题是我在执行的时候一直收到错误。我的IDE是带有EPIC插件的Eclipse,因为我正在学习Lynda教程。
错误是:
Reference found where even-sized list expected at C:/Documents and Settings/nunya/eric.hepperle_codebase/lynda/lamp/perl5/Exercise Files/14 Modules/eh_bibleInspiration_controller.pl line 42.
Use of uninitialized value $value in concatenation (.) or string at C:/Documents and Settings/nunya/eric.hepperle_codebase/lynda/lamp/perl5/Exercise Files/14 Modules/eh_bibleInspiration_controller.pl line 45.
HASH(0x19ad454) => 下面是控制器类:
#!/usr/bin/perl
# eh_bibleInspiration_controller.pl by Eric Hepperle - 06/23/13
#
use strict;
use warnings;
use Data::Dumper;
use EHW_BibleInspiration;
main(@ARGV);
sub main
{
my $o = EHW_BibleInspiration->new; # instantiate new object.
my %bo_ref = $o->getBibleObj();
print "\$o is type: " . ref($o) . ".\n";
print "\%bo_ref is type: " . ref(\%bo_ref) . ".\n";
# exit;
$o->getVerseObj();
listHash(\%bo_ref);
message("Done.");
}
sub message
{
my $m = shift or return;
print("$m\n");
}
sub error
{
my $e = shift || 'unkown error';
print("$0: $e\n");
exit 0;
}
sub listHash
{
my %hash = @_;
foreach my $key (sort keys %hash) {
my $value = $hash{$key};
message("$key => $value\n");
}
}以下是返回诗句的类,并具有挑选随机诗句的方法:
# EHW_BibleInspiration.pm
# EHW_BibleInspiration.
#
package EHW_BibleInspiration;
use strict;
use warnings;
use IO::File;
use Data::Dumper;
our $VERSION = "0.1";
sub new
{
my $class = shift;
my $self = {};
bless($self, $class); # turns hash into object
return $self;
}
sub getVerseObj
{
my ($self) = @_;
print "My Bible Verse:\n";
my $verses = $self->getBibleObj();
# get random verse
#$knockknocks{(keys %knockknocks)[rand keys %knockknocks]};
# sub mysub {
# my $params = shift;
# my %paramhash = %$params;
# }
# my %verses = %{$verses};
# my $random_value = %verses{(keys %verses)[rand keys %verses]};
# print Dumper(%{$random_value});
}
sub getBibleObj
{
my ($self) = @_;
# create bible verse object (ESV)
my $bibleObj_ref = {
'john 3:16' => 'For God so loved the world,that he gave his only Son, that whoever believes in him should not perish but have eternal life.',
'matt 10:8' => 'Heal the sick, raise the dead, cleanse lepers, cast out demons. You received without paying; give without pay.',
'Luke 6:38' => 'Give, and it will be given to you. Good measure, pressed down, shaken together, running over, will be put into your lap. For with the measure you use it will be measured back to you.',
'John 16:24' => 'Until now you have asked nothing in my name. Ask, and you will receive, that your joy may be full.',
'Psalms 32:7' => 'You are a hiding place for me; you preserve me from trouble; you surround me with shouts of deliverance. Selah',
'Proverbs 3:5-6' => 'Trust in the LORD with all your heart, and do not lean on your own understanding. 6 In all your ways acknowledge him, and he will make straight your paths.',
'John 14:1' => 'Let not your hearts be troubled. Believe in God; believe also in me.'
};
my $out = "The BIBLE is awesome!\n";
return $bibleObj_ref;
}
1;我做错了什么?我怀疑这与散列和散列引用有关,但我不知道如何修复它。我的取消引用尝试失败了,因为我真的不知道我在做什么。我根据我在perlmonks上看到的东西来模拟我的随机getter。#$knockknocks{(keys %knockknocks)[rand keys %knockknocks]};
发布于 2013-06-25 21:41:54
大体上,你有:
my %bo_ref = $o->getBibleObj();但是,在package EHW_BibleInspiration;中,getBibleObj方法返回:return $bibleObj_ref;
大体上,你会这样做:my $bo_ref = $o->getBibleObj();
然后调用listHash($bo_ref);
最后,不要忘记将sub listHash更改为:
sub listHash
{
my ($hash) = @_;
foreach my $key (sort keys %{$hash}) {
my $value = $hash->{$key};
message("$key => $value\n");
}
}发布于 2013-06-25 21:09:33
在你的main中,你需要
listHash(\%bo_ref);这会将散列引用传递给sub。然而,它试图像这样解开它的论点
my %hash = @_;哦,它想要一个哈希。
listHash(%bo_ref)。这就省去了我们大量的输入。sub listHash { my ($hashref) = @_;foreach my $key (排序关键字%$hashref) { my $value = $hashref->{$key};print "$key => $value\n";}}
注意引用如何使用取消引用箭头->来访问散列条目,以及如何将其取消引用为keys.的散列
发布于 2013-06-25 21:13:13
我怀疑你的怀疑是正确的。在您的方法sub listHash中,您正确地传递了散列,但是您尝试使用散列而不是内部变量的散列引用。尝试使用my ($hash) = @_;而不是my %hash = @_;。
在使用引用时,您可以使用->运算符解引用它,以获得基础值。方法的其余部分应该如下所示:
sub listHash
{
my ($hash) = @_;
foreach my $key (sort keys %{$hash}) {
my $value = $hash->{$key};
message("$key => $value\n");
}
}在程序的43行,我必须通过调用keys %{$hash}来告诉Perl这个引用应该是一个散列引用。然后,在第44行,我通过调用$hash->{$key}取消了对散列的引用,以获得正确的值。有关Perl和参考资料的更多信息,您可以通过tutorial阅读。
https://stackoverflow.com/questions/17298127
复制相似问题