Hi vishi,
To have more clear understanding for the people who is reading your sample code I wouild like to add some more additional information on the same :-
Hi newbies
Create a file called "MakerDB.pm" by using your favourite editor.
then have the following contents
package MakerDB;
use strict;
use warnings;
use Carp;
use DBI;
use Data::Dumper;
my $dbh;
my $dbobj;
use Class::MethodMaker
get_set => [ { -read_cb => sub {
my $self = shift;
my $dsn = 'DBI:mysql:mydb';
my $dbuser ='root';
my $dbpass = '';
eval {
$dbh=DBI->connect($dsn,
$dbuser,
$dbpass,
{ RaiseError => 1,
AutoCommit => 0 }
);
};
croak "\n\ncould not connect : $@" if $@;
return $dbh;
}
},'connect'
],
new => 'new';
1;
The last line should contain "1;" that is the successful return value from package ( that is missed out from the sample given by Vishi )
Then create any perl file to make use of the module "MakerDb"
The perl file contains the following:-
use strict;
my $vObj = MakerDB->new();
print "\n\tSuccessfully connected to DB\n\n" if my $dbhandle=$vObj-
+>connect();
my $sth=$dbhandle->prepare("SELECT NAME, PASSWORD FROM userlog");
$sth->execute();
my $href;
while ($href=$sth->fetchrow_hashref()) {
print "\n";
print Dumper $href;
}
If you create those two files and run that will work fine as expected. this will be helpful for all the newbies who are all new to Perl also.
thanx.
"Keep pouring your ideas"
2005-10-24 jdporter edited: added code and br tags
|