package Mylibrary; use strict; use DBI; my $database="somedb:localhost"; my $username="username"; my $password="password"; sub connect { my $dbh = DBI->connect("DBI:mysql:$database", "$username","$password") or error_fatal("blah"); return $dbh; } sub get_current_vote { my ($dbh) = @_; # if we're not connected (or weren't passed # a connection), connect $dbh ||= connect(); my $sth = $dbh->prepare("SELECT current_vote FROM vote_count;"); $sth->execute() or die $dbh->errstr(); my $current_vote = ($sth->fetchrow_array())[0]; return $current_vote; } 1;