open PIPE, '-|', 'sort -r ../hosts/iplist.txt | sort -u -t ',' -k 2,2' or die "Cannot open pipe from sort because: $!"; while ( ) { # do your stuff here } close PIPE or warn $! ? "Error closing sort pipe: $!" : "Exit status $? from sort";