in reply to how to debug an 'undef' that cannot be?
Just to re-phrase what my fellow monks already pointed out, your problem is that you have two different $dev variable. When you use "my" in front of a variable, its scope becomes the current block. In the main program it's not a problem but in a while loop like you did, or any other kind of blocks, the variable will cease to exists at the end of said block, and will be recreated for each iteration, and it will never alter the content of the original variable defined outside of this block.
In clear: remove the "my" besides your $dev variable inside your while loop because it creates a separate variable, not related to the one you think you are using.
Testing never proves the absence of faults, it only shows their presence.