#!/usr/bin/perl -w use strict; use warnings; my $ins=0; my @insert1 = qw(1 1 1); my @insert2 = qw(2 2 2); my $outfile = 'delme.txt'; open OUTFILE, '>', $outfile; print OUTFILE <; insert (@insert2); @ARGV = $outfile; $/ = undef; print <>; sub insert{ print "insert called:". $ins ."\n"; $ins++; my @toBeInserted = @_; open(FILE2,$outfile)||die "you stupid fuck, file doesn't exit\n";; open(FILE3,">"."tmp.file")||die "you stupid fuck, file doesn't exit\n";; my $cnt=0; while (){ chomp; my @fields = (split /[ \t]+/,$_); push(@fields,$toBeInserted[$cnt]); foreach(@fields){ my $tmpString = $_ . "\t"; print FILE3 $tmpString; } print FILE3 "\n"; $cnt++; } close(FILE2); close(FILE3); #now swap the files unlink($outfile) || die "Cannont delete old file.\n"; rename("tmp.file", $outfile) || die "Cannot rename file\n"; } #### insert called:0 first second third 1 1 2 3 1 x y z 1 insert called:1 first second third 1 1 2 3 1 x y z 1 2