经过zhong8解释,终于理解了。小试牛刀,写了个简化的数据库链接。
package TDmodule; use DBI; sub new{ $self={}; $self->{username}=undef; $self->{password}=undef; $self->{host}=undef; $self->{database}=undef; $self->{connect_db}=[]; $self->{select_db}=undef; bless $self; return $self; } sub username{ my $self=shift; if(@_){ $self->{username}=shift; } return $self->{username}; } sub password{ my $self=shift; if(@_){ $self->{password}=shift; } return $self->{password}; } sub host{ my $self=shift; if(@_){ $self->{host}=shift; } return $self->{host}; } sub database{ my $self=shift; if(@_){ $self->{database}=shift; } return $self->{database}; } sub select_db{ my $self=shift; if(@_){ $in=DBI->connect("DBI:mysql:database=$self->{database};host=$self->{host}","$self->{username}","$self->{password}") or die "No:$! "; my $select=$in->prepare(@_[0]); $select->execute(); #$self{select_db}=$select->fetchrow_hashref(); $self{select_db}=$select; } return $self{select_db}; } 1;