use strict; use warnings; my (@config); push @config, 'line 1 oldtext appears but once here', 'line 2 oldtext is here twice oldtext', 'line 3 oldtext is here oldtext three oldtext times!'; for (@config) { my $count = 2; while ($count-- && s/oldtext/newtext/) { print 'Replaced '} print "\n"; } print "$_\n" for @config;