my $dbh = DBI->connect( $info{dsn}, $info{db_user}, $info{db_cred}, {AutoCommit => 0, RaiseError => 0, PrintError => 0 }) || die ("Cannot connect: ".$DBI::errstr."\n"); my $flg = get_flg(); $dbh->disconnect(); # Check flg if ($flg == 0 ) { send_email("PASS:$flg\n"); } elsif ($flg == 1) { #Call to query2.sql here send_email("FAIL:$flg\n"); exit; }; $dbh->disconnect; sub get_flg { undef $/; open (my $fh, "< query1.sql") or die "error opening this file $!"; my $sth= $dbh->prepare(<$fh>) || die ("Cannot connect: ".$DBI::errstr."\n"); $sth->execute; close $fh; my $row = $sth->fetchrow_hashref; $sth->finish; return $row->{VAL1}; print $row; } sub send_email { my $message = shift; open (MAIL, "|/usr/sbin/sendmail -t") or die "Error opening sendmail: $!"; print MAIL "To: me\@myhost.com\n"; print MAIL "From: checks"; print MAIL "Subject: Status Checks\n"; print MAIL "\n"; print MAIL $message; close MAIL; }