You could do the following to achieve your aim like so:
tie my @array, 'Tie::File', "$the_dir$filename", recsep=>']' or die "c
+an't tie file:$!";
print join ']',@array;
Since, you have supplied a recsep as ']', and each record is delimited by it and thus not included in the array element. By
not using
autochomp=>0. Hence, we could insert the ']' using the join function.
Also the last ']' in the end comments are also not included.
Hope this helps.