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; #### Replaced Replaced Replaced Replaced Replaced line 1 newtext appears but once here line 2 newtext is here twice newtext line 3 newtext is here newtext three oldtext times!