use Tie::File; for (glob '*.{c,h}') { tie my @file, 'Tie::File', $_ or warn $! and next; unshift @file, '#include something3.cfg'; }