foreach my $l (`top -b -n 1`){ #or, for that matter, foreach(`top -b -n 1 | head -n 10`)? #the code goes here }