my $file = "mydata.txt"; # read data from file open my $fh, '+<', $file or die "can't open file $file : $!\n"; my @data = <$fh>; chomp @data; # modify data (add item) @data = sort (@data, 'Simon'); # rewind back to beginning seek($fh, 0, 0); for my $d (@data) { # write data back to file (skips blank lines) print $fh "$d\n" if $d; } # if new file is shorter than original, throw away extra truncate($fh, tell($fh)); close $fh;