Tie::File could be used to automatically save changes to the file. This is also useful if the file isn't small enough to fit into memory all at once.
#!/usr/bin/perl
use Tie::File;
my $filename = 'foo.dat';
tie @array, 'Tie::File', $filename or die "Cannot open $filename\n";
$array[2] = '#'.$array[2];
$array[3] = '#'.$array[3];