#!/bin/perl
use strict;
use warnings;
my $input_file = 'input.txt';
open(my $in_file,'<',$input_file) or die "Can not open file $input_file for reading: $!.\n";
if (grep{/Friday/} $in_file) { # WRONG. You are grepping the filehandle,
# not the content of the file.
print "Match\n";
} else {
print "No match\n";
}
print "Before\n"; # "Before" is long ago.
while (<$in_file>) {
print; # you don't look for "Friday" looping lines.
}
print "After\n"; # after "After" nothing happens but file closing.
close($in_file);
####
#!/bin/perl
use strict;
use warnings;
my $input_file = 'input.txt';
open(my $in_file,'<',$input_file) or die "Can not open file $input_file for reading: $!.\n";
while (<$in_file>) {
if (/Friday/) {
warn "match on line $.\n"; # warn goes to STDERR
} elsif ($. == 2) { # no match on line 2
chomp; # remove line ending
$_ .= "Friday\n"; # add "Friday" and line ending
warn "added 'Friday' to line $.\n";
}
print;
}
close($in_file);
####
#!/bin/perl -l -pi.bak
use strict;
use warnings;
if (/Friday/) {
warn "match on line $.\n"; # warn goes to STDERR
} elsif ($. == 2) { # no match but on line 2
$_ .= "Friday"; # add "Friday" (and line ending via -l)
warn "added 'Friday' to line $.\n"; # warn doesn't add line ending via -l
}