in reply to subroutine array empty

my $dir = @_;
That's most likely setting $dir to "1" (scalar context applied), and then skipping the rest of the subroutine because you don't have a directory named "1".