Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Executing script in normal/daemon mode and adding start/stop capability by chanakya

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (4)
    As of 2018-07-15 23:25 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (328 votes). Check out past polls.