Possible Duplicate:
Perl + POO and “Can't call method ”prepare”
I learned poo and i got to play with perl, create this module but when I call the segudno method I skip the following error ‘Use of uninitialized value $database in concatenation ( .) … ‘ Followed by ‘Can’t call method ‘prepare’ mm i don’t really understand, any suggestions?
#!/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;
in other file for call msg and created objected.
#!/usr/bin/perl
use MysqlTest;
use warnings;
use strict;
my $mysqltest = MysqlTest->new();
$mysqltest->conexion("bookmarks", "localhost", "root", "pass");
$mysqltest->consulta();
this output in console.
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.
any idea?
thanks.
Your code includes this line:
You call your
conexionsub with no arguments. However, this sub expects several arguments, including a blessed object, that you don’t provide. You might want to fix that.$databaseand$hostnamealso are expected in the arguments.Your call to
conexionwill always be executed, becausemy $varcreates a new variable and initializes it withundef—and the negation ofundefis a true value.Then you have this statement:
mycreates a new variable$connecton which you try to call the methodprepare. This doesn’t work, as the created variable is no blessed reference but simplyundef.Scope in Perl
Here is a verbose example on how lexical scope with
myworks in PerlYou define a
myvariablemysqlopeninconexion, then redefine it inconsulta. It is, however, not the same variable, as these variables reside in different scope.Instead, you are likely to add fields
mysqlopenandconnectin the object you are passing around, as this is object data. You can then use this information just like thehostordatabasefields.Also, do not call the method
conexionwithout an object. Objects are usually created withnew.Edit
It is quite difficult for me to debug your code and parse your English at the same time, so here is your code with the worst bugs removed. However, I have no experience with DBI, so it may not work directly: