9

聖書の一節を表示するために作成した Perl クラス/モジュールがあります。その中には、いくつかの詩を格納するハッシュがあり、キーは本/章/詩であり、値はテキストです。このハッシュはモジュールから返されます。

私はコントローラークラスに聖書クラスを含めていますが、その接続は機能しているようです。問題は、実行時にエラーが発生し続けることです。Lynda のチュートリアルに従っているため、私の IDE は EPIC プラグインを備えた Eclipse です。

エラーは次のとおりです。

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 で見たものからランダムゲッターをモデル化しました。 #$knockknocks{(keys %knockknocks)[rand keys %knockknocks]};

4

4 に答える 4

8

主に、次のものがあります。

 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-25T13:41:54.280 に答える
6

あなたのmain中で、あなたは

listHash(\%bo_ref);

これにより、ハッシュ参照がサブに渡されます。ただし、次のように引数を展開しようとします

my %hash = @_;

おっと、ハッシュが必要です。

  1. いずれにせよ、ハッシュを渡します: listHash(%bo_ref). これにより、入力の手間が大幅に省けます。
  2. または、次のように、サブ内で参照を処理します

    sub listHash {
      my ($hashref) = @_;
      foreach my $key (sort keys %$hashref) {
        my $value = $hashref->{$key};
        print "$key  => $value\n";
      }
    }
    

    参照が逆参照矢印->を使用してハッシュ エントリにアクセスする方法と、それが のハッシュに逆参照される方法に注意してくださいkeys

于 2013-06-25T13:09:33.050 に答える
0

listHashハッシュを期待しており、ハッシュ参照を渡しています。変化する:

listHash(\%bo_ref);

に:

listHash(%bo_ref);
于 2013-06-25T13:10:01.177 に答える