ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm writing a Perl script to connect to a database:
Quote:
my $dbh = DBI->connect('DBI:Oracle:host=**,sid=***',
'usrname',
'passwd',
{
RaiseError => 1,
AutoCommit => 0
}
) || die "Database connection not made:
$DBI::errstr";
What I want is, when the database connection is NOT set up, the script should email me to let me know. But since the die command is there to terminate the program right after the unsuccessful connection, how do I add the command, like 'sendmail'?
I'm writing a Perl script to connect to a database:
What I want is, when the database connection is NOT set up, the script should email me to let me know. But since the die command is there to terminate the program right after the unsuccessful connection, how do I add the command, like 'sendmail'?
#******************************************************************************
#
# Function : send_email
#
# Description : Send an email warning for errors
# Can be called from exit_with_error(), so don't call
# exit_with_error(), otherwise infinite loop.
#
# Params : $_[0] error msg
#
# Returns : none
#
#******************************************************************************
sub send_email
{
my (
$error_msg, # input error msg
$hostname, # name of this box for sending
$prog_full_name, # prog path + name
$contact_email, # contact email address
$mailer, # mailer object
$sender, # sender of email as specified in cfg file
$subject, # subject line in email
$body_text # actual email msg text
);
# Assign input params
$error_msg = $_[0];
# Get hostname anyway we can using Sys::Hostname;
# tries syscall(SYS_gethostname), `hostname`, `uname -n`
$hostname = Sys::Hostname::hostname();
# Get program full path name
$prog_full_name = cwd()."/${0}";
# Set the fields required by Mailer...
$contact_email = $cfg::params{'ADMIN_EMAIL'};
$sender = "$prog_full_name";
$subject = "$prog_full_name: Error trapped";
$body_text = "This is an automated message:\n\n".
"$error_msg\n\n".
"Regards,\n$prog_full_name at $hostname\n\n";
# ... and send it
$mailer = Mail::Mailer->new();
$mailer->open({ From => $sender,
To => $contact_email,
Subject => $subject,
}) or print "Can't open Mailer for send: $!\n";
print $mailer $body_text;
close($mailer) or print "Can't close Mailer: $!\n";
}
You'll need
Code:
use Mail::Mailer; # Access sendmail
use Sys::Hostname; # Used to insert hostname in emails
use Cwd; # Get current full dir for error msgs
Also, you're using wrong type of 'or':
'||' is for inside if( $a || $b) constructs. For 'or die' eg file/db open, use 'or' eg
Code:
$file="test.txt";
open( TXT_FILE, "<$file" ) or
die "Can't open txt file: $file: $!\n";
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.