in reply to how to debug an 'undef' that cannot be?

... the fact that it happens randomly is a bit disconcerting...

... especially since, for the reason kielstirling has already pointed out, I don't see any way that  $dev could possibly be anything other than undefined at that point in the given code!