package MyDB; use strict; use DBI; use Config::Tiny; use Data::Dumper; sub new { my $class = shift; my $self = {}; my $file = shift || 'config.cfg'; my $cnf = Config::Tiny->read($file); my $dbh = DBI->connect("DBI:mysql:dbname=$cnf->{database}->{DATABASE};host=$cnf->{database}->{IP};port=$cnf->{database}->{PORT}", $cnf->{database}->{USER}, $cnf->{database}->{PASS}, {RaiseError=>1, mysql_enable_utf8 => 1}); $self->{dbh} = $dbh; bless $self,$class; return $self; } sub get_sites { my $self = shift; my $sth = $self->{dbh}->prepare("SELECT * from sites"); $sth->execute(); $self->{sites} = $sth->fetchall_arrayref({}); } 1; #### #!/usr/bin/perl use strict; use MyDB; use Data::Dumper; my $dbh = MyDB->new('config.cfg'); my $test = $dbh->get_sites(); print Dumper $test;