If I was troubleshooting this, I would sprinkle print statements liberally in the code and log them. Even start with print "1\n"; print "2\n";, etc. Then isolate what part of the code
gets executed, and where it hangs.
in reply to unexpected quit - no error message
I know with "devices", code can hang so badly it will lock up the software, sort of in a "wait and retry" state.
You may have to isolate the line, wrap it in an eval, with a timeout, and check what the eval error is. Google for "perl eval error" and you will find may examples.