I have text file ,results.txt which has 2 things:

1.a newline character

2.a string "hello"

The following script of mine opens the results,reads each line .If it finds for the the string "hello", it sends a mail.If it does not find "hello". it does nothing.But the problem the mail is sent for both cases.Can you please figure whats the problem in the foll in script:

#!/usr/bin/perl use MIME::Lite; $pattern ='hello'; my $msg = MIME::Lite->new( From => 'rajyalakshmi.bangar@eternoinfotech.com', To => 'bangar.rajyalakshmi@gmail.com', Type => 'multipart/mixed', Subject => "Fetch issues as on $dateFile'", ); $msg->attach( Type => 'TEXT', Data => "Hi,Please find attachement for Fetch issues as on '$d +ateFile'", ); $msg->attach( Type => 'text', Path => 'results.txt', Filename => 'results.txt', ); #sysopen($FILE,$filepath,o_RDWR) or die "could find the file"; open FILE, "<results.txt" or die "Can't open results output file: $!"; while (my $line=<FILE>) { if($line=~m/\b$pattern/) { print " match"; $msg->send; } }

The mail is send for both foll conditions:

  1. if($line=~m/\b$pattern/)
  2. if($line!~m/\b$pattern/)

ie:ideally if match is found and the conditon is if($line!~m/\b$pattern/),the mail should not be sent

But if remove the new line character from the results.txt,the conditions work fine


In reply to problem while searching for a pattern in a file by rajyalakshmi

Title:
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, details, 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, summary, 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.