我很难过。我是Perl的新手,在阅读了一些文章后,我仍然无法弄清楚这一点。这是一个非常小的班级。Perl类中没有代码引用
package Haha;
sub new {
$class = shift;
$self = {
path => shift
};
bless $self, $class;
return $self;
}
sub setPath {
my ($self, $new_path) = shift;
$self->(path) = $new_path if defined $new_path;
return $self->(path);
}
sub getPath {
my $self = shift;
return $self->(path);
}
1;
我用它像这样:
use lib 'lib';
use Haha;
my $new_excel = new Haha("sample path");
print $new_excel->getPath() ;
<>;
类哈哈线23发出 “没有一个代码参考” 错误。 ,说return $self->(path);
请加'use strict;对你的文件使用warnings'并用'my'定义词法变量。当访问对象散列字段时,也使用大括号而不是括号:'$ self - > {path} = $ new_path' –
'$ self - >(path)'暗示$ self是代码引用; '$ self - > {path}'=>散列引用; '$ self-> path' =>调用'path()'方法的祝福对象。请检查https://perldoc.perl.org/perlref.html#Making-References –