#!/usr/bin/env perl use warnings; use strict; use File::Temp qw/tempfile/; use File::Basename qw/fileparse/; my $filename = "/tmp/test.html"; my @to_insert = ( '

Hello,', 'World! It is '.gmtime(time).' UTC

' ); open my $ifh, '<', $filename or die "open $filename: $!"; my ($basename,$path) = fileparse($filename); my ($tfh,$tfn) = tempfile( $basename.'_XXXXXXXXXX', SUFFIX=>'.tmp', DIR=>$path ); #warn "Debug: writing to $tfn\n"; # change the lines of the input file however you wish here my $found; while (<$ifh>) { print $tfh $_; if (//i) { $found=1; print $tfh "$_\n" for @to_insert; } } close $ifh; close $tfh; chmod(0666&~umask,$tfn) or warn "Couldn't chmod $tfn: $!"; if (!rename($tfn, $filename)) { my $e=$!; unlink($tfn); die "Renaming $tfn to $filename failed: $e"; } #warn "Debug: Renamed $tfn to $filename\n"; die "Marker not found" unless $found;