4
我只是用Perl初學者,所以如果這聽起來愚蠢的 - 對不起爲:)如何在Perl構造器中定義一個空數組?
我的問題是 - 我試圖寫一個類,它有一個空的陣列,在類的構造函數中定義。所以我是這樣做的:
package MyClass;
use strict;
sub new {
my ($C) = @_;
my $self = {
items =>()
};
bless $self, ref $C || $C;
}
sub get {
return $_[0]->{items};
}
1;
後來我測試我的類簡單的腳本:
use strict;
use Data::Dumper;
use MyClass;
my $o = MyClass->new();
my @items = $o->get();
print "length = ", scalar(@items), "\n", Dumper(@items);
,當運行腳本,我得到以下幾點:
$ perl my_test.pl
length = 1
$VAR1 = undef;
爲什麼我我做錯了什麼導致我得到我的items
陣列充滿undef
?
也許有人可以告訴我怎麼樣的類需要定義,所以我不會得到我的數組中的任何默認值?
謝謝!現在一切正常 – Laimoncijus 2010-04-25 07:03:14