diff --git a/src/main/java/org/perlonjava/runtime/operators/Directory.java b/src/main/java/org/perlonjava/runtime/operators/Directory.java index f75a70017..95cc77eb4 100644 --- a/src/main/java/org/perlonjava/runtime/operators/Directory.java +++ b/src/main/java/org/perlonjava/runtime/operators/Directory.java @@ -252,7 +252,10 @@ public static RuntimeScalar mkdir(RuntimeList args) { try { Path path = RuntimeIO.resolvePath(fileName); - Files.createDirectories(path); + // Use createDirectory (not createDirectories) so it throws FileAlreadyExistsException + // when the directory exists. This matches Perl's behavior where mkdir() fails + // with EEXIST if the directory already exists. + Files.createDirectory(path); // Set permissions only if the file system supports POSIX permissions if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) { @@ -263,9 +266,9 @@ public static RuntimeScalar mkdir(RuntimeList args) { return scalarTrue; } catch (IOException e) { - // Set $! (errno) in case of failure - getGlobalVariable("main::!").set(e.getMessage()); - return scalarFalse; + // Set $! (errno) properly using handleIOException which maps + // FileAlreadyExistsException to EEXIST (17), etc. + return handleIOException(e, fileName, 0); } } }