my $comp_name = get_comp_name($hash{comp_id}); BEGIN { my $sth; sub get_comp_name { my $comp_name = shift; $sth = $dbh->prepare("select comp_name from company where comp_name = ?") unless $sth; my $r_name = $sth->selectrow_array($sth, undef, $comp_name); return $r_name; } }