Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:50:37+00:00 2026-05-29T22:50:37+00:00

I am writing this simple code to get alert email when "error" found on

  • 0

I am writing this simple code to get alert email when "error" found on my log.

The question is I want to get only one alert per scan my code is generate number of alert based number of error matched on the file.

open (LOG, "<$log") || die 'Could not OPEN log file';
while ($loglines = <LOG> ) {
if ($loglines =~ /Error/) { 
    print "Error on the log \n";
}
 }
 close (LOG);

the result looks like

Error on the log

Error on the log

Error on the log

Error on the log

Error on the log

Instead I want just one print "Error on the log" that i way I can get one email when error matched on the log file. What did I miss?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:50:40+00:00Added an answer on May 29, 2026 at 10:50 pm
    open my $LOG, "<", $log or die "Could not OPEN log file $log";
    while (my $loglines = <$LOG>) {
      if ($loglines =~ /Error/) { 
       print "Error on the log\n";
       last;
      }
    }
    close($LOG);
    

    There are a lot of improvements in that. The most important change is the addition of last;; it terminates the loop on the first match.

    Other changes are:

    • Use of lexical file handle.
    • Use of three-argument open.
    • Use of ‘or‘ instead of ‘||‘.
    • Local variable for $loglines (assuming you didn’t need/use it elsewhere).
    • Error message identifies which file could not be opened (easier to debug!).
    • No trailing space on the printed message line.

    Some would prefer a label added to the loop and then an explicit use of that in the last statement:

    open my $LOG, "<", $log or die "Could not OPEN log file $log";
    LOG_READER:
    while (my $loglines = <$LOG>) {
      if ($loglines =~ /Error/) { 
       print "Error on the log\n";
       last LOG_READER;
      }
    }
    close($LOG);
    

    I’m not convinced there is much gain here, assuming there are no enclosing loops. If there are any enclosing loops, then use the label.

    I assume you are using:

    use strict;
    use warnings;
    

    at the top of your script? If not, then do so. Perl experts use them to make sure they avoid making silly mistakes; Perl novices should use them for the same reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be simple. Question How do you get a pointcut in one project
Is this a simple process? I'm only writing a quick hacky UI for an
I am writing a simple batch file and i get this The syntax of
I'm writing this question with reference to this one which I wrote yesterday. After
This might be a simple answer, but I am having some issues writing this
What I'm trying to do this is writing a simple parser for the following
I'm writing this question in the spirit of answering your own questions, since I
I'm writing this question from the standpoint of an ASP.NET application. However I realize
Unfortunately I am not writing this question from my Developing PC so I might
I am writing a code that I am sure is very simple, but for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.