use strict; use warnings; # Get file to process (my $file, my $pattern, my $replacement) = @ARGV; my $Count = 0; # Read file open my $FH, "<", $file or die "Unable to open $file for read exited $ +? $!"; chomp (my @lines = <$FH>); close $FH; # Parse and replace text in same file open $FH, ">", $file or die "Unable to open $file for write exited $? +$!"; for (@lines){ say {$FH} $_ unless (/$pattern/); #Write to file unchanged say {$FH} $_ if (s/$pattern/$replacement/g); #Write to file change +d if ($_ =~ /$pattern/) { #Need to check & count multiple occur +rences in same line $Count = $Count + 1; #Need to increment correctly print $Count; #Wanted to test but will not print return $Count; #Need to get count back to main } } close $FH; print $Count;