Puns抛在一边,我试图在我的Perl类中实现一个导入方法来实例化一个数据对象,这个实例本质上是一个很好的散列数组,来自一个合适的hashref数组.
以下是我计划如何使用它的示例:
# Pull in the data my $data = Data->import( [ { a => 1,b => 7,c => 3},{ a => 7,b => 9,c => 2},] ); $data->manipulate; # Use package methods
我的进口实施如下:
package Data; sub initialize { my $class = shift; my $data = []; bless $data,$class; return $data; } sub import { my ( $class,$data ) = @_; bless $data,$class; return $data; } 1;
令人惊讶的是,Perl在编译时报告错误(注意BEGIN块):
Can't bless non-reference value at Data.pm line 51. BEGIN Failed--compilation aborted at myScript.pl line 8.
perldiag
对于发生了什么没有太清楚:
Can’t bless non-reference value
(F)
Only hard references may be blessed. This is how Perl “enforces”
encapsulation of objects. See
07001.
我甚至尝试初始化对象并在两个单独的步骤中添加数据:
sub import { #< Another constructor > my ( $class,$data ) = @_; my $obj = $class->initialize; push @$obj,@$data; return $obj; }
这导致以下编译时错误:
Can't use an undefined value as an ARRAY reference... BEGIN Failed--compilation aborted at...
两个问题:
>我做了什么事情怎么了?
有人可以澄清这个编译时错误的perldiag解释吗?