0
我有一個目錄中的某些文件。面向對象的Perl:調用同一類中的另一個函數
所以,我使用的非面向對象的Perl代碼如下(只是重要的片段如下印刷):
#!/usr/bin/perl
use strict;
use warnings;
my $dnaFilesDirectory = "./projectGeneSequencingPfzr";
my %properties = &returnGeneticSequences($dnaFilesDirectory);
sub returnGeneticSequences {
my $dnaDirectory = shift;
my @dnaFiles =();
opendir(DNADIR, $dnaFilesDirectory) or die "Cannot open directory:$!";
@dnaFiles = readdir(DIR);
foreach my $file (@dnaFiles) {
my $dnaFilePath = $dnaFilesDirectory."\/".$file;
if($file =~ /dna_file.*\.dnaPrj/) {
my %diseaseStages = &returnDiseasesStages($dnaFilePath);
## Do some data analysis on the %diseaseStages Hash;
}
}
}
sub returnDiseasesStages {
my $dnaFile = shift;
## Do something with DNA file and build a hash called %diseasesStagesHash;
return %diseasesStagesHash;
}
上面的代碼工作正常。
但是我們必須爲上述功能創建相應的OO Perl代碼。
我正在嘗試執行以下操作,但似乎無法正常工作。顯然,我在從returnGeneticSequences
調用類方法returnDiseasesStages
時做錯了什麼。
#!/usr/bin/perl
use strict;
use warnings;
package main;
my $obj = GeneticSequences->new(dnaFilesDir => "./projectGeneSequencingPfzr");
$obj->returnGeneticSequences();
package GeneticSequences;
sub new {
my $class = shift;
my $self = {
dnaFilesDir => "dnaFilesDir",
@_,
};
return (bless($self,$class));
}
sub returnGeneticSequences {
my $self = shift;
my $dnaFilesDirectoryGS = $self->{dnaFilesDir};
my @dnaFiles =();
opendir(DNADIR,$dnaFilesDirectoryGS) or die "Cannot open directory:$!";
@dnaFiles = readdir(DIR);
foreach my $file (@dnaFiles) {
my $dnaFilePath = $dnaFilesDirectory."\/".$file;
if($file =~ /dna_file.*\.dnaPrj/) {
my $gsObj = GeneticSequences->new();
my %diseaseStages = $gsObj->returnDiseasesStages($dnaFilePath);
## Do some data analysis on the %diseaseStages Hash;
}
}
}
sub returnDiseasesStages {
my $dnaFile = shift;
##Do something with DNA file and build a hash called %diseasesStagesHash;
return %diseasesStagesHash;
}
請幫我理解我做錯了什麼。
試圖改變你的問題弄成短,使得更多的人可以看看它:http://www.sscce.org/ – mrks