in reply to textwrap of file

Just a pointer but you do this:
local $/= undef; # read entire file at once my @text=<FH>;

The local $/ line is unncessary because you are then reading the file line by line into an array (or at least that is what you want to do). Really, you should pick what you want, to slurp or not to slurp. For example:
my $data; open(FH,'<',$file) || die "Oops $!"; local $/ = undef; $data = <FH>; close(FH);
Here, you now have to split up your data as you see fit. By doing what you have done, you are slurping the whole of the contents of the file into the first element of the array. To see this, run the following program on a test file and uncomment the 'local' line:
use strict; use warnings 'all'; # Uncomment this to see what I mean # local $/ = undef; open(FH,'<',"test.txt") || die "oops: $!"; my @data = <FH>; close (FH); print $data[0];

Ok granted that the wrap call needs an array of lines but its intelligent enough to work with lines split on \n boundaries.