in reply to if ( -d ) question

As it specifies in -X, -d and its brethren act relative to the current working directory. If $mydir is not ".", then you have the wrong file list. By specifying $mydir."/$_" (or "$mydir/$_"), you are giving a full path. You can also swap your current working directory with chdir, but that may have side effects you are not expecting.