readline() on closed filehandle is coming when executing the perl script. I am trying to get the number of line in file which contain the X. I am not getting where is the issue.
#!/usr/bin/perl
use strict;
use warnings;
my ($filename, $lines_num, $line, $c);
$lines_num = 0;
$filename = "input.txt";
open my $in, "<", $filename;
while ($line = <$in>)
{
$c = substr($line, 0, 1);
if (lc($c) eq "x")
{
$lines_num++;
}
}
close($in);
print "In " , $filename, " there are ",
$lines_num, " lines that start with \"X\".\n";
my input file contains as below
X
XX
XXX
XXXX
XXXXX
XXXXXX
XXXXXXX
XXXXXXXX
XXXXXXXXX
XXXXXXXXXX
XXXXXXXXXXX
XXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXXXX
XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX
And my output show "readline() on closed filehandle $in at filehandleoperator.pl line 12."