Dear Monks
I have a bunch of scripts which monitor incoming email, process them and load parsed data into Mysql.
I'm demonizing the scripts, in order to process the incoming emails at a faster rate. I have implemented
Proc::Daemon to achieve this. All is well till now.
I'm planning to add modes (like normal mode or daemon mode) to the script, so that I can execute
scripts in daemon mode via the cron, and use the normal mode if I want to troubleshoot or manually
load some emails.
In my code below I have added the daemon mode via the command line options.
Here I need your suggestion on how can i simplify the code and also remove duplicated code. The code shown is a sample script, and the code for some of the scripts is long.
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Proc::Daemon;
use Log::Log4perl qw(get_logger :easy :levels);
Use MysqlUtils;
use vars qw($opt_daemon, $opt_mailid);
GetOptions(
"daemon|d" => \$opt_daemon,
"mailid|a=s" => \$opt_mailid,
"help|h" => \&usage,
) || usage();
if(!$opt_mailid){
$opt_mailid = shift || usage();
}
chomp($opt_mailid);
&usage if( (!defined($opt_mailid)) && (@ARGV==0) );
if(scalar(@ARGV) > 0){
if ( (!defined ($opt_mailid)) || (@ARGV > 0) ) {
usage();
die("Invalid Argument passed");
}
}
if($opt_daemon) {
##Inits
print STDOUT "Daemon mode \n";
Proc::Daemon::Init;
Log::Log4perl->init_once("$ENV{CONF}" . "/log4perl.conf");
my @mail_ids = qw(200909101 200909102 200909103);
while (1) {
foreach my $opt_mailid (@mail_ids) {
my ($log) = Log::Log4perl::get_logger('main');
my $data = get_data('mail_id' => $opt_mailid, 'file_path' => "
+/spool/emails");
##connect db
MysqlUtils::connect();
eval {
MysqlUtils::insert_record($data);
};
if($@) {
$log->error("unable to insert data");
}
}
}
}
else {
print STDOUT "Normal mode\n";
my ($log) = Log::Log4perl::get_logger('main');
my $data = get_data('mail_id' => $opt_mailid, 'file_path' => "/spo
+ol/emails");
##connect db
MysqlUtils::connect();
eval {
MysqlUtils::insert_record($data);
};
if($@) {
$log->error("unable to insert data");
}
}
The above code works perfectly in the daemon mode and normal mode.
Here's my two questions and seeking suggestions to improve the code
* How can avoid code duplication
* How can I add start/stop commands for the script.
thank you for your time
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.