Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To avoid code duplication, use a subroutine.

Also you will find your code easier to read an debug if you use consistent indenting. The block of code controlled by a flow-of-control statement should always be indented inside the flow of control statement. It is also a good idea to be consistent in your choice of cuddled and uncuddled curly braces.

A third bit of advice, $@ can sometimes be set to undef even if there is an error. Even so, eval will always return undefined when there is an error. A much better way to check for errors is the incantation:

eval { # my stuff ... #Note: return *only* exits eval {...} #make sure success "returns" true return 1; } or do { # what you are currently putting inside if ($@) {...} goes here. }

With a subroutine and the above eval incantation, your code (indenting revised):

if($opt_daemon) { # ... intro stuff 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' => "/spool/emails"); ##connect db MysqlUtils::connect(); eval { MysqlUtils::insert_record($data); }; if($@) { $log->error("unable to insert data"); } }

becomes

sub runMailHandler { my ($opt_mailid) = @_; 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); return 1; } or do { $log->error("unable to insert data"); } } if($opt_daemon) { # intro stuff ... while (1) { runMailHandler($_) foreach @mail_ids; } } else { runMailHandler($opt_mailid); }

Best, beth

Update Added comments about eval handling.


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

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found