Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 444563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:16:59+00:00 2026-05-12T21:16:59+00:00

I have part of a build process that creates a hideously long paths in

  • 0

I have part of a build process that creates a hideously long paths in Windows. It’s not my fault. It’s several directories deep, and none of the directory names are abnormally long; they’re just long and numerous enough to make it over MAX_PATH (260 chars). I’m not using anything other than ASCII in these names.

The big problem is that the blow-up happens deep in the guts of Module::Build during the dist target, although I figure the build system doesn’t matter because they’d make the same directories.

Creating one of these overly-long directories with File::Path fails:

 use File::Path qw( make_path );

 make_path( 'C:\\.....' ); # fails if path is over 260 chars

Similarly, constructing each directory level by hand fails once the absolute path would go over MAX_PATH.

This isn’t new, isn’t Perl’s fault, and Microsoft documents it in Naming Files, Paths, and Namespaces. Their fix suggests adding the \\?\ in front of any path to access the Unicode filename API. However, that doesn’t seem to be the full fix for a Perl script because it still fails:

 use File::Path qw( make_path );

 make_path( '\\\\?\\C:\\.....' );  # still fails if path is over MAX_PATH, works otherwise

This might be because make_path pulls apart its argument and then goes through the directories one level at a time, so \\?\ only applies to the top-level, which is within MAX_PATH.

I dug up a bug report to ActiveState that suggests there’s something else I need to fix up to get to the Unicode filenames, and Jan Dubois gives a bit more details in Re: “long” filenames on Windows 2K/XP, although I’m not sure it applies (and is extremely old). perlrun mentions that this use to be the job of the -C switch, but apparently that part was abandoned. The perl RT queue has a more recent bug 60888: Win32: support full unicode in filenames (use Wide-system calls).

Miyagawa notes some Unicode filename issues and Win32API::File without specifically mentioning long paths. However, the Win32API::File CPAN Forum entry seems to indicate only fear, which leads to anger, which leads to hate, and so on. There’s an example in the Perlmonks post How to stat a file with a Unicode (UTF16-LE) filename in Windows?. It seems the Win32::CreateDirectory is the answer, and I’ll try that the next time I get next to a Windows machine.

Then, supposing I can create the long path path. Now I have to teach Module::Build, and maybe other things, to handle it. That might be immediately easy with monkeypatches if Win32::GetANSIPathName() does what it says on the tin.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:16:59+00:00Added an answer on May 12, 2026 at 9:16 pm

    Windows has two separate system call for each function that needs to deal with strings, an "A" call using the ANSI aka Active Code Page as the encoding (e.g. cp1252) and a "W" call using UTF-16le. Perl uses "A" calls, while \\?\ only works with "W" calls.

    You can use Win32::API to access the "W" calls as shown in the script below, but Win32::LongPath not only uses the "W" calls, but automatically adds \\?\!

    Example of using Win32::API to call CreateDirectoryW to use a long path (\\?\-prefixed path):

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Carp;
    use Encode qw( encode );
    use Symbol;
    
    use Win32;
    
    use Win32API::File qw(
        CreateFileW OsFHandleOpen
        FILE_GENERIC_READ FILE_GENERIC_WRITE
        OPEN_EXISTING CREATE_ALWAYS FILE_SHARE_READ
    );
    
    use Win32::API;
    use File::Spec::Functions qw(catfile);
    
    Win32::API->Import(
        Kernel32 => qq{BOOL CreateDirectoryW(LPWSTR lpPathNameW, VOID *p)}
    );
    
    my %modes = (
        '<' => {
            access => FILE_GENERIC_READ,
            create => OPEN_EXISTING,
            mode   => 'r',
        },
        '>' => {
            access => FILE_GENERIC_WRITE,
            create => CREATE_ALWAYS,
            mode   => 'w',
        },
        # and the rest ...
    );
    
    use ex::override open => sub(*;$@) {
        $_[0] = gensym;
    
        my %mode = %{ $modes{$_[1]} };
    
        my $os_fh = CreateFileW(
            encode('UCS-2le', "$_[2]\0"),
            $mode{access},
            FILE_SHARE_READ,
            [],
            $mode{create},
            0,
            [],
        ) or do {$! = $^E; return };
    
        OsFHandleOpen($_[0], $os_fh, $mode{mode}) or return;
        return 1;
    };
    
    my $path = '\\\\?\\' . Win32::GetLongPathName($ENV{TEMP});
    my @comps = ('0123456789') x 30;
    
    my $dir = mk_long_dir($path, \@comps);
    my $file = 'test.txt';
    my $str = "This is a test\n";
    
    write_test_file($dir, $file, $str);
    
    $str eq read_test_file($dir, $file) or die "Read failure\n";
    
    sub write_test_file {
        my ($dir, $file, $str) = @_,
    
        my $path = catfile $dir, $file;
    
        open my $fh, '>', $path
            or croak "Cannot open '$path':$!";
    
        print $fh $str or die "Cannot print: $!";
        close $fh or die "Cannot close: $!";
        return;
    }
    
    sub read_test_file {
        my ($dir, $file) = @_,
    
        my $path = catfile $dir, $file;
    
        open my $fh, '<', $path
            or croak "Cannot open '$path': $!";
    
        my $contents = do { local $/; <$fh> };
        close $fh or die "Cannot close: $!";
        return $contents;
    }
    
    sub mk_long_dir {
        my ($path, $comps) = @_;
    
        for my $comp ( @$comps ) {
            $path = catfile $path, $comp;
            my $ucs_path = encode('UCS-2le', "$path\0");
            CreateDirectoryW($ucs_path, undef)
                or croak "Failed to create directory: '$path': $^E";
        }
        return $path;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 254k
  • Answers 254k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since you can't change the UITableViewStyle of a UITableView once… May 13, 2026 at 10:02 am
  • Editorial Team
    Editorial Team added an answer Short of implementing a custom title bar and non-client area,… May 13, 2026 at 10:02 am
  • Editorial Team
    Editorial Team added an answer This is one of the problems with the IEEE 754… May 13, 2026 at 10:02 am

Related Questions

We are using continuous integration as part of our build automation. For every check
I'm writing a game development IDE that creates and compiles .NET projects (which I've
I want to disallow people from cluttering our source tree with generated CMake files...
We have a GUI of several frames that build their contents dynamically. Each frame

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.