【发布时间】:2018-11-17 13:18:42
【问题描述】:
我想创建一个 Perl 类。
这是我的代码:
#!/usr/ bin/perl
package Data;
sub new
{ my ($class, $student) = @_;
$class = shift;
$student = shift;
$student = {_firstName => <>,
_lastName => <>,
_matric => <>,
};
bless $student,$class;
print $student;
}
&new;
我得到了这个错误:
main=HASH(0x215fc40) 在 source_file.pl 第 12 行 bless 中使用未初始化的值 $class。 在 source_file.pl 第 12 行明确祝福 ''(假设 package main)。
我使用 dcoder,我认为它不支持分离程序。但是尽管我纠正了它,但它仍然没有输出。我试图打印$self,但它留下了一个错误。完整代码如下:
#!/usr/ bin/perl
use strict;
use warnings 'all';
package student;
sub new {
my $class = shift;
my ($first, $last, $matric) = @_;
my $self = {
_firstName => $first,
_lastName => $last,
_matric => $matric,
};
bless $self, $class;
}
my $self = student->new('victor','valdes','csc');
print $self;
这是错误:
学生=HASH(0x238ae98)
很抱歉,我对此很陌生,仍然不明白。
【问题讨论】:
-
一切正常。
student=HASH(0x238ae98)表示您有一个student对象,该对象由地址0x238ae98处的哈希形成。你需要做更多的阅读。