我有一个对象具有另一组对象的数组的属性。我有一个 toString 方法,我想打印出对象的全部内容。主要目标是让 Job 对象调用数组中的所有后处理作业。我也想在对象数组中的对象上调用 toString 方法。目前,我收到此错误:
Can't call method "toString" without a package or object reference at JobClass.pm line 52, <JOBFILE> line 5. (which is $item->toString(); in the foreach loop)
$item 上的 Dumper 显示以下内容:
$VAR1 = bless( {
'ImportID' => '22',
'ImportTableID' => '1234',
'ImportTable' => 'testImport'
}, 'PostJob' );
我试图理解的主要目标是如何调用从成员数组返回的对象的方法。
以这种方式实例化的类:
我的 $postJob = PostJob->new(ImportTable => "testImport",ImportTableID => "1234", ImportID => "22");
我的@postJobs =“”;
推(@postJobs,$postJob);
$postJob->toString(); #这很好用
我的 $job = Job->new(DirectoryName => "testDir",StagingTableName => "stageTable", QBStagingTableID => "5678",postProcessJobs => \@postJobs);
$job->toString(); #Breaks 上面有错误
代码如下:
打包 PostJob;
使用驼鹿;
使用严格;
使用 Data::Dumper;
有 'ImportTable' => (isa => 'Str', is => 'rw', required => 1);
有 'ImportTableID' => (isa => 'Str', is => 'rw', required => 1);
有 'ImportID' => (isa => 'Str', is => 'rw', required => 1);
子字符串 {
# 打印所有值
我的 $self = 班次;;
print "Post Job 的表名是 ".$self->ImportTable."\n";
print "Post Job 的表 ID 是 ".$self->ImportTableID."\n";
print "Post Job 的导入 ID 是 ".$self->ImportID."\n";
}
打包作业;
使用严格;
使用 Data::Dumper;
使用驼鹿;
有 'DirectoryName' => (isa => 'Str', is => 'rw', required => 1);
有 'StagingTableName' => (isa => 'Str', is => 'rw', required => 1);
有 'StagingTableID' => (isa => 'Str', is => 'rw', required => 1);
有 'postProcessJobs'=> (isa => 'ArrayRef', is => 'rw', required => 0);
子 addPostJob {
我的 ($self,$postJob) = @_;
推(@{$self->postProcessJobs()},$postJob);
}
子字符串
{
# 打印所有值。
我的 $self = 班次;
print "转储作业对象内容**********************************\n";
print "目录是".$self->DirectoryName."\n";
print "Staging Table is ".$self->StagingTableName."\n";
print "Staging Table ID is ".$self->StagingTableID."\n";
打印“转储岗位工作内容**********************************\n”;
foreach 我的 $item (@{$self->postProcessJobs()})
{
$item->toString();
打印自卸车($项目);
}
打印“结束转储作业*******************************\n”;
}
1个;