cody
cody

Reputation: 137

pattern searching in perl

use strict; 
use warnings;

open(FILE1, "/cygdrive/c/cpros/mola.txt");
my $line = <FILE1>;
print $line;
close(FILE1);
open(FILE1, ">/cygdrive/c/cpros/mola.txt");

if ($line = ~ /karthik/)
{
  print FILE1 ("1");
}
else
{
  print FILE1 ("0");
}

close(FILE1);

I have stored hello world in mola.txt file but still its printing 1 but the pattern karthik is not saved in file but why its printing 1 ? how to make search patterns?

Upvotes: 0

Views: 352

Answers (1)

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43498

=~ is a separate operator, so it shouldn't have whitespace between its characters. With the whitespace, the condition of the if statement becomes an assignment that always yields true.

Upvotes: 9

Related Questions