yes. that redirection will redirect the output of head, not the grep inside.
I can only suggest you to drop that line and implement it in perl (as you use perl already)
Code:
open (PS, "ps -ef") or die;
while ( <PS> )
{
# evaluate things here
}
close PS;
Another tip is to avoid finding procID if pName is empty (do not execute that line at all).