in reply to Sub only grabbing first line from STDIN

foreach $line (@text) { my $text =$text[$line];
$line doesn't contain an index number of your array, it's the contents of each array element. That is, what you think is in $text[$line] is really in $line

There may be other things wrong with the rest of the script (looks like there's more to it than this). use strict, use -w and if you need 'em, use diagnostics.

update See below for other, more direct answers.