我学会了便便,我开始玩 perl,创建了这个模块,但是当我调用 segudno 方法时,我跳过了以下错误“在连接中使用未初始化的值 $database (.) ...”,然后是“无法调用方法” '准备' mm 我不太明白,有什么建议吗?
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
use DBD::mysql;
package MysqlTest;
sub new{
my $class = shift;
my $query={};
bless($query, $class);
}
sub conexion{
my $self=shift;
my($database, $hostname, $user, $pwd)=@_;
$self->{"host"}=$hostname;
$self->{"database"}=$database;
$self->{"user"}=$user;
$self->{"pass"}=$pwd;
our $connect = DBI->connect("DBI:mysql:database=$database;host=$hostname;", $user, $pwd) or die $DBI::errstr;
my $mysqlopen = 1;
return;
}
sub consulta{
my $self=shift;
if (!my $mysqlopen) { &conexion(); }
my $id = "SELECT * FROM save_bookmarks WHERE id='123'";
our $result = my $connect->prepare($id);
$result->execute();
my @resultado = $result->fetchrow_array();
print "@resultado\n";
return;
}
sub datos{
my $self=shift;
print "::DATOS DE ACCESO::\n";
while ((my $key, my $value)=each(%$self)){
print "$key => $value\n";
}
}
1;
在其他文件中调用 msg 并创建了反对。
#!/usr/bin/perl
use MysqlTest;
use warnings;
use strict;
my $mysqltest = MysqlTest->new();
$mysqltest->conexion("bookmarks", "localhost", "root", "pass");
$mysqltest->consulta();
控制台中的此输出。
Use of uninitialized value $database in concatenation (.) or string at MysqlTest.pm line 23.
Use of uninitialized value $hostname in concatenation (.) or string at MysqlTest.pm line 23.
Can't call method "prepare" on an undefined value at MysqlTest.pm line 31.
任何的想法?
谢谢。