Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t使用自动加载构建perl类_Perl - Fatal编程技术网

Can';t使用自动加载构建perl类

Can';t使用自动加载构建perl类,perl,Perl,类基因 package Gene; use strict; use Carp; use vars qw($AUTOLOAD); my %_ok_gene_attr = ( "id" => "string", "name" => "string", "chrom" => "string", # chromosome or seq id "txtStart" => "int", # 1-based "txtEnd" => "int" # 1-based ); su

类基因

package Gene;
use strict;
use Carp;
use vars qw($AUTOLOAD);

my %_ok_gene_attr = (
"id" => "string",
"name" => "string",
"chrom" => "string", # chromosome or seq id
"txtStart" => "int", # 1-based
"txtEnd" => "int" # 1-based
 );

sub new {
my ($class, %arg) = @_;
my $self = {};
$self->{_id} = $arg{id} || croak "no id";
$self->{_name} = $arg{name} || croak "no db";
$self->{_chrom} = $arg{chrom} || croak "no seq_id";
$self->{_strand} = $arg{strand} || -1;
$self->{_txStart} = $arg{txStart} || -1;
$self->{_txEnd} = $arg{txEnd} || -1;
$self->{_cdsStart} = $arg{cdsStart} || -1;
$self->{_cdsEnd} = $arg{cdsEnd} || -1;
$self->{_exonCount} = $arg{exonCount} || -1;
$self->{_exonStarts} = $arg{exonStarts} || -1;
$self->{_exonEnds} = $arg{exonEnds} || -1;
$self->{_score} = $arg{score} || -1;
$self->{_name2} = $arg{name2} || -1;
$self->{_cdsStartStat} = $arg{cdsStartStat} || -1;
$self->{_cdsEndStat} = $arg{cdsEndStat} || -1;
$self->{_exonFrames} = $arg{exonFrames} || -1;

bless($self, $class);
}


sub AUTOLOAD {
my ($self, $newvalue) = @_;
my ($operation, $attribute) = ($AUTOLOAD =~ /(get)(_\w+)$/);
unless ($operation && $attribute) {
croak "Method name $AUTOLOAD is not in the recognized form get_attribute\n";
}
unless (defined $_ok_gene_attr{$attribute}) {
croak "No such attribute '$attribute' exists in the class ", ref($self);
}
if ($operation eq 'get') {
*{$AUTOLOAD} = sub { shift->{$attribute} };
} elsif($operation eq 'set') {
*{$AUTOLOAD} = sub { shift->{$attribute} = shift };
$self->{$attribute} = $newvalue;
}
return $self->{$attribute};
}

sub DESTROY {
my($self) = @_;
$self->_decr_count( );
}
我在课堂上使用这个类

  open my $in, '<', q/C:\Users\Jesse\Desktop\refGene.txt/ or die "Cannot open file : $!";
while(<$in>) {

    chomp $_;
    my @temp= split(/\t/, $_);
    my $i=0;
    my $temp1= Gene->new("id" => $temp[0],"name"=>$temp[1],"chrom"=>$temp[2], "strand"=>$temp[3],"txStart"=>$temp[4], "txEnd"=>$temp[5],"cdsStart"=>$temp[6],"cdsEnd"=>$temp[7],"exonCount"=>$temp[8],"exonStarts"=>$temp[9],"exonEnds"=>$temp[10],"score"=>$temp[11],"name2"=>$temp[12],"cdsStartStat"=>$temp[13],"cdsEndStat"=>$temp[14],"exonFrames"=>$temp[15]);


}

当我使用$temp1->set_id(“1234”)?时,自动加载功能似乎也不起作用。

试试这种固定版本的
Gene.pm

package Gene;
use strict;
use Carp;
use vars qw($AUTOLOAD);

use Data::Dumper;

my %_ok_gene_attr = (
    "_id" => "string",
    "_name" => "string",
    "_chrom" => "string", # chromosome or seq id
    "_txtStart" => "int", # 1-based
    "_txtEnd" => "int" # 1-based
);

sub new {
    my ($class, %arg) = @_;
    my $self = {};
    $self->{_id} = $arg{id} || croak "no id";
    $self->{_name} = $arg{name} || croak "no db";
    $self->{_chrom} = $arg{chrom} || croak "no seq_id";
    $self->{_strand} = $arg{strand} || -1;
    $self->{_txStart} = $arg{txStart} || -1;
    $self->{_txEnd} = $arg{txEnd} || -1;
    $self->{_cdsStart} = $arg{cdsStart} || -1;
    $self->{_cdsEnd} = $arg{cdsEnd} || -1;
    $self->{_exonCount} = $arg{exonCount} || -1;
    $self->{_exonStarts} = $arg{exonStarts} || -1;
    $self->{_exonEnds} = $arg{exonEnds} || -1;
    $self->{_score} = $arg{score} || -1;
    $self->{_name2} = $arg{name2} || -1;
    $self->{_cdsStartStat} = $arg{cdsStartStat} || -1;
    $self->{_cdsEndStat} = $arg{cdsEndStat} || -1;
    $self->{_exonFrames} = $arg{exonFrames} || -1;

    bless($self, $class);
}


sub AUTOLOAD {
    my ($self, $newvalue) = @_;
    my ($operation, $attribute) = ($AUTOLOAD =~ /(get|set)(_\w+)$/);
    unless ($operation && $attribute) {
        croak "Method name $AUTOLOAD is not in the recognized form set_{attribute} or get_{attribute}\n";
    }
    unless (defined $_ok_gene_attr{$attribute}) {
        croak "No such attribute '$attribute' exists in the class ", ref($self);
    }
    if ($operation eq 'get') {
        no strict 'refs';
        *{$AUTOLOAD} = sub { shift->{$attribute} };
    } elsif($operation eq 'set') {
        no strict 'refs';
        *{$AUTOLOAD} = sub { shift->{$attribute} = shift };
        $self->{$attribute} = $newvalue;
    }
    return $self->{$attribute};
}

sub DESTROY {
    my($self) = @_;
    $self->_decr_count( );
}
使用以下脚本进行测试:

#!/usr/bin/perl

use strict;
use warnings;

use Gene;

my $t1 = Gene->new(id=>"id", name=>"name", chrom=>"chrom");

$t1->set_id("1234");
print $t1->get_id(), "\n";


除了使用Perl的内置OO系统外,您可能还想看看其他一些OO系统,例如。有关更多详细信息,请参阅。

您没有执行
\u decr\u count
设置id
,并且您的
自动加载
也无法识别它们。您希望Perl做什么?我没有使用_decr_count()?我只想创建一个基因对象。自动加载功能是否会自动为每个属性执行获取或设置功能?
autoload
的功能取决于您对它的实现。请检查我的答案。
#!/usr/bin/perl

use strict;
use warnings;

use Gene;

my $t1 = Gene->new(id=>"id", name=>"name", chrom=>"chrom");

$t1->set_id("1234");
print $t1->get_id(), "\n";