Eri*_*010 6 oop perl hash pass-by-reference hashref
我有一个Perl类/模块,我创建它来显示圣经经文.其中有一个存储多个经文的哈希,其中键是书/章/诗,而值是文本.此哈希值从模块返回.
我将圣经类包含在控制器类中,并且该连接似乎有效.问题是我在执行时遇到错误.我的IDE是因为我正在使用Lynda教程,Eclipse是带有EPIC插件的.
错误是:
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) =>
Run Code Online (Sandbox Code Playgroud)
这是CONTROLLER类:
#!/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");
}
}
Run Code Online (Sandbox Code Playgroud)
这是返回经文的类,并有选择随机经文的方法:
# 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;
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我怀疑它与哈希引用和哈希引用有关,但我不知道如何修复它.我的解除引用尝试失败了,因为我真的不知道我在做什么.我模仿了我在perlmonks上看到的随意的吸气剂. #$knockknocks{(keys %knockknocks)[rand keys %knockknocks]};
在主要,你有:
my %bo_ref = $o->getBibleObj();
Run Code Online (Sandbox Code Playgroud)
但是,在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");
}
}
Run Code Online (Sandbox Code Playgroud)
在你main,你做
listHash(\%bo_ref);
Run Code Online (Sandbox Code Playgroud)
这会将散列引用传递给sub.但是,它尝试解压缩其参数
my %hash = @_;
Run Code Online (Sandbox Code Playgroud)
哎呀,它想要哈希.
listHash(%bo_ref).这节省了我们很多打字.或者,我们处理子内部的引用,如
sub listHash {
my ($hashref) = @_;
foreach my $key (sort keys %$hashref) {
my $value = $hashref->{$key};
print "$key => $value\n";
}
}
Run Code Online (Sandbox Code Playgroud)
请注意引用如何使用解除引用箭头->来访问哈希条目,以及如何将其解除引用哈希值keys.