use strict; use warnings; use feature 'say'; while ( my $line = ) { chomp $line; say replace( $line, 'SUBSTR', 'FOO' ); } sub replace { my ( $input, $wanted, $replacement ) = @_; if ($input =~ m/$wanted/) { return $replacement;} else { return $input; } } __DATA__ path/to/some/file path/to/some/other/file path/to/SUBSTR/file #replace entire line if SUBSTRING is found path/to/file #### path/to/some/file path/to/some/other/file FOO path/to/file