http://www.perlmonks.org?node_id=873590

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

Hi monks, i am struggling with editing a file , Iam opening a file and replacing string with a comment(#) at start of that line for example: i have a test.txt file in that there is string called "test" , whenever i found the string test , that line should be commented , here is my code

#!/usr/bin/perl -w my $file_name = "test.txt"; open(INF,'./$file_name') || die "file could not open\n"; my @line; while(@line = <INF>) { if ( $line =~ s/test/^\#/g) { open(INF,'>>$file_name') || die "could not open in writ +e mode"; print INF "Matching is done by $line \n"; close(INF); } else { print "No match found \n"; } close(INF); }