rajyalakshmi has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: problem while searching for a pattern in a file
by cdarke (Prior) on Mar 24, 2010 at 09:05 UTC
    Your file is a strange format, so I created it programtically, if this is incorrect then please say so. I also simplified the code for these purposes, removing the email components:
    #!/usr/bin/perl use strict; use warnings; my $pattern ='hello'; open (my $fh, '>', 'results.txt') or die "results.txt: $!"; binmode $fh; print $fh "\nhello"; close $fh; open FILE, "< results.txt" or die "Can't open results output file: $!" +; while (my $line=<FILE>) { if($line=~m/\b$pattern/) { print " match: <$line>"; } } close FILE;
    This matched "hello" as expected, just once. I suggest that maybe your file is not in exactly the format you say.
    You might like to display it using, for example od -xc results.txt if you are on UNIX/Linux.

    By the way, you are taking a risk running code without warnings and strict set. For example, in the commented out sysopen the flags should be O_RDWR (assuming you use Fcntl;), not o_RDWR, which you won't pickup.
Re: problem while searching for a pattern in a file
by kiruthika.bkite (Scribe) on Mar 24, 2010 at 08:18 UTC
    Hi,
    I have executed your program in my system.It sent the mail only if the pattern get matched.
    I have tested your code with the following input.

    Input

    contents of results.txt
    \n
    \n
    \n
    This is testing

    In this case mail didn't send.

    And,
    if($line!~m/\b$pattern/)

    This condition will match the pattern other than "hello".
    So when newline is encountered this condition will get true.(\n!="hello").
    So in this case also mail will send.