Well, I don't have any suggestions concerning the supposed flaw in your logic. You haven't told us anything about what you're trying to do to be able to tell you where your code is incorrect. However, here is how I would clean up your current code:
#!/usr/bin/perl -w
use strict;
my...